简体   繁体   English

Python pandas:将字典嵌套到数据框的列表

[英]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,score和spread

[[{'hasPossession': '0', 'score': '23', 'spread': '-0'}, [[{{'hasPossession':'0','score':'23','spread':'-0'},
{'hasPossession': '0', 'score': '34', 'spread': '0.0'}], [{'hasPossession': '0', 'score': '', 'spread': '-7.5'}, {'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':'7.5'}],[{'hasPossession':'0','score':'','spread':'-1 '},
{'hasPossession': '0', 'score': '', 'spread': '1.0'}]] {'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. 一般地,上述结构是一个包含3名列表列表 ,并且每个列表中包含2 字典与2个元素。

How do I transform such into pandas dataframe? 如何将其转换为熊猫数据框?

flatten the list and use the default constructor flatten列表并使用默认构造函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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