1

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?

1
  • i think it is list of dict right , can you show us the structure of your list ? Commented May 17, 2018 at 22:47

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.