简体   繁体   English

从词典列表创建Pandas Dataframe,进行解析

[英]Creating a Pandas Dataframe from a list of dictionaries, parsing

I have a Python list of dictionaries, with string and integer values. 我有一个包含字符串和整数值的Python字典列表。

I would like to import this into a Pandas DataFrame. 我想将此导入到Pandas DataFrame中。 My first thought was to manipulate the list of dictionaries into one big dictionary, and then import this into a Pandas DataFrame. 我的第一个想法是将字典列表处理成一个大词典,然后将其导入到Pandas DataFrame中。

The "one big dictionary" would be “一本大字典”就是

dict_countries = { 'countries':       [],
                   'pop':             [],
                   'capital_city':    [],
                   'national_anthem': [] }

And then I could use 然后我可以使用

for dictionary in list_countries:
    dict_countries['countries'].append(dictionary['country'])
    dict_countries['pop'].append(dictionary['population'])
    dict_countries['capital_city'].append(dictionary['capital'])
    dict_countries['national_anthem'].append(dictionary['anthem'])

However, I am worried that this is a bad idea. 但是,我担心这是一个坏主意。 A dictionary of lists is a bit fragile: if any of the lists get out of synch the whole thing becomes a mess. 列表字典有点脆弱:如果列表中的任何一个不同步,整个事情就会变得一团糟。

How could I skip the middle step and immediately parse my list of dictionaries into a pandas DataFrame? 如何跳过中间步骤,立即将字典列表解析为pandas DataFrame?

A DataFrame can be constructed directly from a list of dictionaries: 可以直接从字典列表中构造一个DataFrame:

In [100]: pd.DataFrame([{'foo':1, 'bar':2}, {'foo':3,'baz':4}])
Out[100]: 
   bar  baz  foo
0    2  NaN    1
1  NaN    4    3

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

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