【问题标题】:ValueError: Mixing dicts with non-Series may lead to ambiguous orderingValueError:将字典与非系列混合可能会导致排序不明确
【发布时间】:2019-11-22 22:09:49
【问题描述】:
import json
import pandas as pd

data = json.load(open('drug-label-0001-of-0008.json'))
df = pd.DataFrame(data)
import pandas as pd
pd_example = pd.read_json('some_json_file.json')
  • 我的代码类似,但出现以下错误:
import pandas as pd
df = pd.read_json('drug-label-0008-of-0008.json')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-77b3c3e486fc> in <module>
----> 1 df = pd.read_json('drug-label-0008-of-0008.json')

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression)
    425         return json_reader
    426 
--> 427     result = json_reader.read()
    428     if should_close:
    429         try:

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in read(self)
    535             )
    536         else:
--> 537             obj = self._get_object_parser(self.data)
    538         self.close()
    539         return obj

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in _get_object_parser(self, json)
    554         obj = None
    555         if typ == 'frame':
--> 556             obj = FrameParser(json, **kwargs).parse()
    557 
    558         if typ == 'series' or obj is None:

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in parse(self)
    650 
    651         else:
--> 652             self._parse_no_numpy()
    653 
    654         if self.obj is None:

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in _parse_no_numpy(self)
    869         if orient == "columns":
    870             self.obj = DataFrame(
--> 871                 loads(json, precise_float=self.precise_float), dtype=None)
    872         elif orient == "split":
    873             decoded = {str(k): v for k, v in compat.iteritems(

~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
    390                                  dtype=dtype, copy=copy)
    391         elif isinstance(data, dict):
--> 392             mgr = init_dict(data, index, columns, dtype=dtype)
    393         elif isinstance(data, ma.MaskedArray):
    394             import numpy.ma.mrecords as mrecords

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/construction.py in init_dict(data, index, columns, dtype)
    210         arrays = [data[k] for k in keys]
    211 
--> 212     return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    213 
    214 

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/construction.py in arrays_to_mgr(arrays, arr_names, index, columns, dtype)
     49     # figure out the index, if necessary
     50     if index is None:
---> 51         index = extract_index(arrays)
     52     else:
     53         index = ensure_index(index)

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/construction.py in extract_index(data)
    318 
    319             if have_dicts:
--> 320                 raise ValueError('Mixing dicts with non-Series may lead to '
    321                                  'ambiguous ordering.')
    322 

ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.

【问题讨论】:

    标签: python json python-3.x pandas dataframe


    【解决方案1】:

    您可以只使用 python 内置的 JSON 处理功能:

    import json
    
    with open("drug-label-0008-of-0008.json", "r") as read_file:
        data = json.load(read_file)
    

    “当您在 json 文件中有单个 JSON 结构时,请使用 read_json,因为它将 JSON 直接加载到 DataFrame 中。使用 json.loads,您必须将其加载到 python 字典/列表中,然后加载到DataFrame - 一个不必要的两步过程。Pandas vs JSON library to read a JSON file in Python"

    【讨论】:

      猜你喜欢
      • 2021-01-05
      • 2021-12-25
      • 2021-08-20
      • 2018-09-05
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 2020-04-24
      相关资源
      最近更新 更多