Issue
I have a nested list looked liek this:
[[{'aaa': '42'}, {'bbb': '60'}, {'ccc': '25'}, {'ddd': '14'}, {'eee': '15'}, {'eee': '84'}],
[{'aaa': '4'}, {'bbb': '0'}, {'ccc': '25'}, {'ddd': '1'}, {'eee': '1'}, {'eee': '8'}]]
And I want to convert it into a Dataframe. I looked at many solutions on our portal but nothing works for me :( Please help me as I'm a fresher in Python and Pandas module. Thank you!
Here is my desired Output:
I'm doing something like this but it's not working for me: DataFrame(data_dicts)
Solution
Assuming L
your input list, you can use a list/dictionary comprehension:
df = pd.DataFrame([{k:v for d in l for d in l for k,v in d.items()} for l in L])
NB. note that you have duplicated keys in your dictionaries, the last ones take precedence
output:
aaa bbb ccc ddd eee
0 42 60 25 14 84
1 4 0 25 1 8
input:
L = [[{'aaa': '42'}, {'bbb': '60'}, {'ccc': '25'}, {'ddd': '14'}, {'eee': '15'}, {'eee': '84'}],
[{'aaa': '4'}, {'bbb': '0'}, {'ccc': '25'}, {'ddd': '1'}, {'eee': '1'}, {'eee': '8'}]]
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.