[英]Faster way to convert Pandas dataframe into nested json
I have data that looks like this: 我有看起来像这样的数据:
player, goals, matches
ronaldo, 10, 5
messi, 7, 9
I want to convert this dataframe into a nested json, such as this one: 我想将此数据帧转换为嵌套的json,例如:
{
"content":[
{
"player": "ronaldo",
"events": {
"goals": 10,
"matches": 5
}
},
{
"player": "messi",
"events": {
"goals": 7,
"matches": 9
}
}
]
}
This is my code, using list comprehension: 这是我的代码,使用列表理解:
df = pd.DataFrame([['ronaldo', 10, 5], ['messi', 7, 9]], columns=['player', 'goals', 'matches'])
d = [{'events': df.loc[ix, ['goals', 'matches']].to_dict(), 'player': df.loc[ix, 'player']} for ix in range(df.shape[0])]
j = {}
j['content'] = d
This works, but the performance is really slow when I have a lot of data. 这行得通,但是当我有很多数据时,性能确实很慢。 Is there a faster way to do it?
有更快的方法吗?
Use pandas.to_json
. 使用
pandas.to_json
。 Fast and easy https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html 快速简便的https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html
df.T.to_json()
try : 尝试:
df.to_json(orient = "records")
The problem is it doesn't stack goals and matches on the event column , I'm not sure though if you can do it without looping 问题是它没有在事件列上堆积目标和匹配项,但我不确定是否可以不循环而完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.