Pablo Brenner
Pablo Brenner

Reputation: 193

Convert a Python list to a multi-column Pandas Dataframe

Folks,

I have a huge Python list which I created from a MongoDB query, it looks like this:

documents[0]['person']

Output:

{'phone': '3368988989898989898',
 'name': 'PABLO',
 'age': 27}

I wish to create a dataframe that looks like a traditional database:

PHONE | NAME | AGE

336...| PABLO| 27

335...| PEDRO| 32

How can I convert this list to a Pandas dataframe so I can write it to my relational database?

Upvotes: 1

Views: 128

Answers (1)

BENY
BENY

Reputation: 323326

IIUC with pd.DataFrame and apply(pd.Series)

documents=[{'person':{'phone': '3368988989898989898',
 'name': 'PABLO',
 'age': 27}},{'person':{'phone': '3368988989898989898',
 'name': 'PEDRO',
 'age': 35}}]

pd.DataFrame(documents).person.apply(pd.Series)
Out[920]: 
   age   name                phone
0   27  PABLO  3368988989898989898
1   35  PEDRO  3368988989898989898

Upvotes: 1

Related Questions