0

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?

userPyGeo
  • 3,631
  • 4
  • 14
  • 24

1 Answers1

2

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
rafaelc
  • 57,686
  • 15
  • 58
  • 82