Reputation: 581
I have the following json structure that I'm trying to parse:
[
{
"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"
]
}....]
There's thousands of values in this JSON-file and I want to parse it into a panda dataframe. How would I go about doing that considering there's a nested list under the ingredients key.
Cheers :)
Upvotes: 1
Views: 792
Reputation: 71620
OR:
pd.concat(map(pd.DataFrame,json))
Example:
>>> 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
>>>
Upvotes: 3
Reputation: 323396
This is list of dict not 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
Upvotes: 1