简体   繁体   中英

How to create a Pandas DataFrame from a list of OrderedDicts?

I have the following list:

o_dict_list = [(OrderedDict([('StreetNamePreType', 'ROAD'), ('StreetName', 'Coffee')]), 'Ambiguous'),
           (OrderedDict([('StreetNamePreType', 'AVENUE'), ('StreetName', 'Washington')]), 'Ambiguous'),
           (OrderedDict([('StreetNamePreType', 'ROAD'), ('StreetName', 'Quartz')]), 'Ambiguous')]

And like the title says, I am trying to take this list and create a pandas dataframe where the columns are: 'StreetNamePreType' and 'StreetName' and the rows contain the corresponding values for each key in the OrderedDict.

I have done some searching on StackOverflow to get some guidance on how to create a dataframe, see here but I am getting an error when I run this code (I am trying to replicate what is going on in that response).

from collections import Counter, OrderedDict
import pandas as pd

col = Counter()
for k in o_dict_list:
    col.update(k)

df = pd.DataFrame([k.values() for k in o_dict_list], columns = col.keys())

When I run this code, the error I get is: TypeError: unhashable type: 'OrderedDict'

I looked up this error, here , I get that there is a problem with the datatypes, but I, unfortunately, I don't know enough about the inner workings of Python/Pandas to resolve this problem on my own.

I suspect that my list of OrderedDict is not exactly the same as in here which is why I am not getting my code to work. More specifically, I believe I have a list of sets, and each element contains an OrderedDict. The example, that I have linked to here seems to be a true list of OrderedDicts.

Again, I don't know enough about the inner workings of Python/Pandas to resolve this problem on my own and am looking for help.

I would use list comprehension to do this as follows.

pd.DataFrame([o_dict_list[i][0] for i, j in enumerate(o_dict_list)])

See the output below.

 StreetNamePreType  StreetName
0   ROAD            Coffee
1   AVENUE          Washington
2   ROAD            Quartz

extracting the OrderedDict objects from your list and then use pd.Dataframe should work

values= []
for i in range(len(o_dict_list)):
    values.append(o_dict_list[i][0])

pd.DataFrame(values)


    StreetNamePreType   StreetName
0   ROAD    Coffee
1   AVENUE  Washington
2   ROAD    Quartz
d = [{'points': 50, 'time': '5:00', 'year': 2010}, 
{'points': 25, 'time': '6:00', 'month': "february"}, 
{'points':90, 'time': '9:00', 'month': 'january'}, 
{'points_h1':20, 'month': 'june'}]

pd.DataFrame(d)

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.

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