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?

1 Answer 1

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

1 Comment

RafaelC, perfect solution you got there.

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.