python - 当我尝试使用 python pandas 模块将 json 转换为数据框时出现 ValueError
问题描述
我有一个嵌套的 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转换为数据框
解决方案
推荐阅读
- maven - 检索 Maven 托管库的公共 pgp 密钥
- module - Monorepo 中汇总配置的 Svelte 热模块重新加载设置
- java - 尝试获取 pivotCacheDefinition 时无法解析类 org.apache.poi.ooxml.POIXMLDocumentPart
- python - Defaultdict 自动为每个键创建一个子字典?
- html - 使用 BeautifulSoup 从 URL 抓取数据并将其保存到 csv
- recaptcha-v3 - 带附件的文件不发送
- discord.py - 如何让嵌入与 discord.py 一起使用?
- java - 有人可以帮我处理这段代码吗,它没有返回我想要的结果
- r - 将箱线图与 Wilcoxon 检验进行比较
- javascript - 在不使用 gmail smtp 的情况下发送电子邮件 nodemailer