0

I'm trying to expand nested json to pandas dataframe, but the offered solutions haven't worked for me.

My case is:

I have json data in MongoDB.

When I connect with this database, it works well, but the problem is when I need to expand nested json to pandas dataframes, because there are columns where I have json object.

index   id    name         location
0       0001  George       [{'country':'Colombia', 'department': 'Antioquia', 'city': 'Medellin'}]
1       0002  Gilberth     [{'country':'Colombia', 'department': 'Antioquia', 'city': 'Medellin'}]
2       0003  Christopher  [{'country':'Colombia', 'department': 'Antioquia', 'city': 'Medellin'}]      

I need to convert to dataframe something like this:

index   id    name         country   department  city
0       0001  George       Colombia  Antioquia   Medellin
1       0002  Gilberth     Colombia  Antioquia   Medellin
2       0003  Christopher  Colombia  Antioquia   Medellin     

Here I show how to read json data:

json_documents = list(properties.find({}))
df = pd.DataFrame(json_documents)
df.head(2)

Thanks in advance.

2 Answers2

1

Use:

df2=df['location'].apply(lambda x: pd.DataFrame(x)) 
pd.concat([df[df.columns[:-1]],df2],axis=1)

Also you can try json_normalize

from pandas.io.json import json_normalize
df=json_normalize(json_documents) 
ansev
  • 30,322
  • 5
  • 17
  • 31
0

Did you try.

df["location"].apply(pd.Series)
Florian Bernard
  • 2,561
  • 1
  • 9
  • 22