简体   繁体   中英

Python pandas: Nested List of Dictionary into Dataframe

Here is how the data structure is below... It is a List with inner List that contains two Dictionaries each.

I want it into dataframe with these headings: hasPossession, score and spread .

[[{'hasPossession': '0', 'score': '23', 'spread': '-0'},
{'hasPossession': '0', 'score': '34', 'spread': '0.0'}], [{'hasPossession': '0', 'score': '', 'spread': '-7.5'},
{'hasPossession': '0', 'score': '', 'spread': '7.5'}], [{'hasPossession': '0', 'score': '', 'spread': '-1'},
{'hasPossession': '0', 'score': '', 'spread': '1.0'}]]

Generally, above structure is a List that contains 3 Lists and each List contains 2 Dictionary with 2 elements.

How do I transform such into pandas dataframe?

flatten the list and use the default constructor

pd.DataFrame([k for item in initial_list for k in item])

    hasPossession   score   spread
0   0               23      -0
1   0               34      0.0
2   0                       -7.5
3   0                       7.5
4   0                       -1
5   0                       1.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM