create a list of separate dictionaries to pandas dataframe

Viewed 32

I have a list in this format

a=[{'a':'123'},{'b':'name1'},{'c':'login'},{'a':'234'},{'b':'name2'},{'c':'Logout'}]

and I want to convert the above list to pandas dataframe in the below format

a     b     c
123 name1 login
234 name2 logout

Can anyone tell me how to convert?

1 Answers

Create dictionary of lists by collections.defaultdict and pass to DataFrame constructor:

from collections import defaultdict

d = defaultdict(list)

for x in a:
    for k, v in x.items():
        d[k].append(v)

df = pd.DataFrame(d)
print (df)
     a      b       c
0  123  name1   login
1  234  name2  Logout
Related