I have a Python list of dictionaries, with string and integer values.
I would like to import this into a Pandas DataFrame. My first thought was to manipulate the list of dictionaries into one big dictionary, and then import this into a 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?
A DataFrame can be constructed directly from a list of dictionaries:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.