Dictionary of list of dictionaries to pandas DataFrame

Viewed 16

I am trying to turn a dictionary of the following type:

{'123': [ {'feature_1':'a1','feature_2':'b1,...},{'feature_1':'a2','feature_2':'b2,...},...,{'feature_1':'an','feature_2':'bn} ],... } to a pandas DataFrame.

My best attempt looks like this:

df = pd.DataFrame.from_dict({(key, lis_index, sub_key): data[key][lis_index][sub_key]
                        for key in data.keys()
                        for lis_index in range(len(data[key]))
                        for sub_key in data[key][lis_index].keys()},
                       orient='index')

where data is my dictionary. However, this leaves 2 columns, one of which a triplet and the other one is a value column. I want to turn it in the following format:

key feature_1 feature_2 ... feature_n
123    a1         b1          z1
123    a2.        b2.         z2
...   ...        ...          ...
123    an         bn          zn
1 Answers

Use list comprehension with add key dictianary to values dict, last pass to DataFrame:

data = {'123': [ {'feature_1':'a1','feature_2':'b1'},
         {'feature_1':'a2','feature_2':'b2'},
         {'feature_1':'an','feature_2':'bn'}] }


df = pd.DataFrame([{**{'key':k}, **x} for k, v in data.items() for x in v])
print (df)
   key feature_1 feature_2
0  123        a1        b1
1  123        a2        b2
2  123        an        bn
Related