[英]Parsing Nested JSON with Pandas
I know that there have been questions similar to this, but I've not yet been able to figure out how to do what I need to. 我知道有类似的问题,但是我还无法弄清楚该怎么做。 I'm trying to take some JSON and move it into a Pandas DataFrame.
我正在尝试使用一些JSON并将其移动到Pandas DataFrame中。
{
"friends": [
{
"name": "Joe Jimmy",
"timestamp": 1541547573
},
{
"name": "Steven Peterson",
"timestamp": 1541274647
}
]
}
I'd like the corresponding DataFrame to look like this: 我希望相应的DataFrame看起来像这样:
Name Timestamp
1 "Joe Jimmy" "1541547573"
2 "Stephen Peterson" "1541274647"
I think the problem is that first nested "friends," but I'm not sure, as I'm new to JSON (and Pandas, really). 我认为问题是首先嵌套了“朋友”,但是我不确定,因为我是JSON(和Pandas的新手)。
I've tried bringing it in via 我尝试过通过
with open('data.json') as f:
friends = json.load(f)
And then moving it to a dataframe via the Panadas DataFrame constructor, but I'm not getting out anything but this: 然后通过Panadas DataFrame构造函数将其移动到数据框,但是我什么也没得到:
{'name': 'Brian B.S. Sheehan', 'timestamp': 15...}
Here is a solution with pandas read_json
: 这是熊猫
read_json
的解决方案:
df = pd.read_json(r'C:\path\data.json')
df.friends.apply(pd.Series)
name timestamp
0 Joe Jimmy 1541547573
1 Steven Peterson 1541274647
Try this one. 试试这个。 http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.json.json_normalize.html Or just use read_json function and use groupby method
http://pandas.pydata.org/pandas-docs/stable/generation/pandas.io.json.json_normalize.html或仅使用read_json函数并使用groupby方法
Try this: 尝试这个:
from pandas.io.json import json_normalize
from json import load
data_json = load(open("inp.json", "r"))
print(json_normalize(data_json,'friends'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.