如何将大熊猫DF转换为这种JSON结构?
假设我有熊猫df
这样:
| name | color of clothing | age |occupation|
| John | yellow | 34 | janitor |
| Carl | red | 27 | doctor |
| Claire | green | 33 | teacher |
| Lisa | blue | 21 | Student |
|........|...................| ....|..........|
我想以这样的json格式进行改造:
[
{ "name": "John,
"color of clothing": "yellow",
"age":"34",
"occupation": "janitor"
},
{ "name": "Carl",
"color of clothing": "red",
"age":"27",
"occupation": "doctor"
},
{ "name": "Claire",
"color of clothing": "green",
"age":"33",
"occupation": "teacher"
},
{ "name": "Lisa",
"color of clothing": "blue",
"age":"21",
"occupation": "student"
}
]
我该怎么做?我最近的比赛是使用df.to_json(orient =“ index”)
,但是我得到的是这样的结构:
{"0":{ "name": "John,
"color of clothing": "yellow",
"age":"34",
"occupation": "janitor"
}
},
"1":{ "name": "Carl",
"color of clothing": "red",
"age":"27",
"occupation": "doctor"
}}
...
真的很感谢任何帮助!
Let's say I have a pandas df
like this:
| name | color of clothing | age |occupation|
| John | yellow | 34 | janitor |
| Carl | red | 27 | doctor |
| Claire | green | 33 | teacher |
| Lisa | blue | 21 | Student |
|........|...................| ....|..........|
I would like to transform it in a json format like this:
[
{ "name": "John,
"color of clothing": "yellow",
"age":"34",
"occupation": "janitor"
},
{ "name": "Carl",
"color of clothing": "red",
"age":"27",
"occupation": "doctor"
},
{ "name": "Claire",
"color of clothing": "green",
"age":"33",
"occupation": "teacher"
},
{ "name": "Lisa",
"color of clothing": "blue",
"age":"21",
"occupation": "student"
}
]
How can I do this? The nearest match I had was using df.to_json(orient="index")
but what I've got was a structure like this:
{"0":{ "name": "John,
"color of clothing": "yellow",
"age":"34",
"occupation": "janitor"
}
},
"1":{ "name": "Carl",
"color of clothing": "red",
"age":"27",
"occupation": "doctor"
}}
...
Would be really thankful for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
df.to_dict('Records')
Use
df.to_dict('records')