I have a list of dicts
[{a:'jeffrey',b:'pineapple',c:'apple'},{a:'epstein',c:'banana'},{a:'didnt kill'},{a:'himself',b:'jebus'}]
What I want to do is transition those values to a pandas df. But as you can see a few dicts are lacking a few keys and therefore lacking values. So I took a glance at defaultdict object so I could transform the list object to something that pandas actually is able to interpret. And transform it into a dataframe.
dd = defaultdict(list)
for d in l:
for k in d.keys():
dd[k]
for d in l:
for k in dd.keys():
try:
dd[k].append(d[k])
except KeyError:
dd[k].append(0)
# Dict auto adaptavél
The code works, and folows the order given of those events meaning with the key is empty return a 0. But I was wondering if there better alternative or a code which has a better o(n) complexity
Wanted result:
defaultdict(<class 'list'>, {'a': ['jeffrey', 'epstein', 'didnt kill', 'himself'], 'b': ['pineapple', 0, 0, 'jebus'], 'c': ['apple', 'banana', 0, 0]})
You can use DataFrame
constructor and fill missing values with 0 then use to_dict
method to export the dataframe as a dict of lists:
>>> pd.DataFrame(l).fillna(0).to_dict('list')
{'a': ['jeffrey', 'epstein', 'didnt kill', 'himself'],
'b': ['pineapple', 0, 0, 'jebus'],
'c': ['apple', 'banana', 0, 0]}
Intermediate result:
>>> pd.DataFrame(l)
a b c
0 jeffrey pineapple apple
1 epstein NaN banana
2 didnt kill NaN NaN
3 himself jebus NaN