我尝试解析以下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文件中有成千上万个值,我想将其解析为熊猫数据框。考虑到在成分键下有一个嵌套列表,我将如何去做。
干杯:)
答案 0 :(得分:3)
OR:
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
>>>
答案 1 :(得分:1)
这是字典而不是json的列表
pd.concat([pd.DataFrame(x) for x in js])
Out[156]:
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