尝试使用python模块将json转换为数据帧时出现Getting ValueError

2024-09-25 10:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个嵌套的json。我正在尝试将这个json转换为pandas中的数据帧

import json
import pandas as pd

with open('scratch.json') as f:
    data = json.load(f)

df = pd.DataFrame(data)

print(df)

我正在犯错误

Traceback (most recent call last):
  File "/Users/nitesh/Library/Preferences/PyCharmCE2019.3/scratches/nitesh.py", line 8, in <module>
    df = pd.DataFrame(data)
  File "/Users/nitesh/.local/share/virtualenvs/nitesh-ovXpZdv7/lib/python3.7/site-packages/pandas/core/frame.py", line 435, in __init__
    mgr = init_dict(data, index, columns, dtype=dtype)
  File "/Users/nitesh/.local/share/virtualenvs/nitesh-ovXpZdv7/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 254, in init_dict
    return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "/Users/nitesh/.local/share/virtualenvs/nitesh-ovXpZdv7/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 64, in arrays_to_mgr
    index = extract_index(arrays)
  File "/Users/nitesh/.local/share/virtualenvs/nitesh-ovXpZdv7/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 365, in extract_index
    raise ValueError("arrays must all be same length")
ValueError: arrays must all be same length

如何将嵌套json转换为数据帧


Tags: inpyjsonsharepandasdataindexlocal