在 Pandas 日期框中读取嵌套 JSON 时遇到问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41814828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trouble reading nested JSON in pandas dateframe
提问by S Coe
I'm new to pandas and python and am having trouble working through this. I've got a complex nexted json file I want to load into a pandas dataframe.
我是Pandas和蟒蛇的新手,在解决这个问题时遇到了麻烦。我有一个复杂的 nexted json 文件,我想加载到 Pandas 数据帧中。
I'm using the following code:
我正在使用以下代码:
import json
import urllib.request
import pandas as pd
import numpy as np
from pandas.io.json import json_normalize
file_str = 'C:\file.json'
with open(file_str, 'r', encoding="utf-8") as json_file:
json_work = pd.read_json(json_file, typ='series', orient='columns')
for k, v in json_work.items():
if v is None:
json_work[k] = "N/A"
##df = pd.DataFrame.from_dict(json_work)
df = pd.io.json.json_normalize(json_work)
print(df)
As it's written I get this error:
正如它所写的那样,我收到了这个错误:
Traceback (most recent call last):
File "C:/.....hack.py", line 18, in <module>
df = pd.io.json.json_normalize(json_work)
File "C:\Users\scoe\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\json.py", line 708, in json_normalize
if any([isinstance(x, dict) for x in compat.itervalues(data[0])]):
File "C:\Users\scoe\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\compat\__init__.py", line 175, in itervalues
return iter(obj.values(**kw))
AttributeError: 'str' object has no attribute 'values'
if I swap these two lines to read
df = pd.DataFrame.from_dict(json_work)
##df = pd.io.json.json_normalize(json_work)
the process runs successfully but the result does not look like a dataframe. The output displays like this:
该过程运行成功,但结果看起来不像数据帧。输出显示如下:
---- more lines above this, its a sample of the middle of the output ----
hrCenterName KW App Development & Maint
hrSignatureLevel 1H
hrSignatureLevelTitle Level 1 HR Signature Authority
imName @
imProvider N/A
... ...
primaryOfficePhoneExtension N/A
---- more lines after this ----
What am I doing wrong?
我究竟做错了什么?
回答by MaxU
json_normalize()expects dict or list of dicts
...
json_normalize()期望dict or list of dicts
...
try this:
尝试这个:
with open(file_str, 'r', encoding="utf-8") as json_file:
json_work = json.load(json_file)
...
df = pd.io.json.json_normalize(json_work)
回答by epattaro
I usually use pandas to read JSON and it works just fine, give it a try:
我通常使用 Pandas 来读取 JSON 并且它工作得很好,试一试:
pd.read_json(path)