[英]Transform json data into a dataframe
I tried to transform JSON data into a dataframe with the following code:我尝试使用以下代码将 JSON 数据转换为 dataframe:
import json
l = []
for line in open('data.json', 'r'):
l.append(json.loads(line))
df = pd.DataFrame(l)
df.parameters.head(1))
l looks like this:我看起来像这样:
{"user_id":0,"client":"35.181","parameters":[{"key":"from","value":"9.204444;45.486389"},{"key":"to","value":"200"},{"key":"with","value":"Train"},
And df looks like this: df 看起来像这样:
user_id client ... parameters
0 30112 ... [{'key': 'from', 'value': '9.29''}, {'key': 'to', 'value': '200'}, {'key': 'with', 'value': 'Train'}]
1 30113 ... [{'key': 'from', 'value': '9.20''}, {'key': 'to', 'value': '30'}, {'key': 'with', 'value': 'Car'}]
And I would like to be able to break the parameters column into 3 distinct columns which would be: from, to, with.我希望能够将参数列分成 3 个不同的列,它们是:从、到、与。
user_id client error ... from to with
0 30112 yes ... 9.29 200 Train
1 30113 NaN ... 9.20 30 Car
Could you help me, please?请问你能帮帮我吗?
Use list comprehension with DataFrame constructor:将列表理解与 DataFrame 构造函数一起使用:
m = df['parameters'].notna()
df1 = pd.DataFrame([{y['key']: y['value'] for y in x} for x in df.pop('parameters').dropna()],
index=df.index[m])
df = df.join(df1)
import json
import pandas as pd
with open('refactory.json') as f:
data = json.load(f)
print(data)
df = pd.read_json('refactory.json')
df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.