Hews
Hews

Reputation: 581

How to parse json into panda dataframe

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

Answers (2)

U13-Forward
U13-Forward

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

BENY
BENY

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

Related Questions