gpt4 book ai didi

python - 如何将 json 解析为 Pandas 数据框

转载 作者:太空宇宙 更新时间:2023-11-04 09:39:15 26 4
gpt4 key购买 nike

我尝试解析以下 json 结构:

[
{
"id": 0,
"cuisine": "greek",
"ingredients": [
"romaine lettuce",
"black olives",
"feta cheese crumbles"
]
},
{
"id": 1,
"cuisine": "southern_us",
"ingredients": [
"plain flour",
"ground pepper",
"milk",
"vegetable oil"
]
}....]

此 JSON 文件中有数千个值,我想将其解析为 Pandas 数据框。考虑到 ingredients 键下有一个嵌套列表,我将如何去做。

干杯 :)

最佳答案

或者:

pd.concat(map(pd.DataFrame,json))

例子:

>>> import pandas as pd
>>> json=[
{
"id": 0,
"cuisine": "greek",
"ingredients": [
"romaine lettuce",
"black olives",
"feta cheese crumbles"
]
},
{
"id": 1,
"cuisine": "southern_us",
"ingredients": [
"plain flour",
"ground pepper",
"milk",
"vegetable oil"
]
}]
>>> pd.concat(map(pd.DataFrame,json))
cuisine id ingredients
0 greek 0 romaine lettuce
1 greek 0 black olives
2 greek 0 feta cheese crumbles
0 southern_us 1 plain flour
1 southern_us 1 ground pepper
2 southern_us 1 milk
3 southern_us 1 vegetable oil
>>>

关于python - 如何将 json 解析为 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52267088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com