Home > OS >  python pandas: add dictionaries of different length as columns
python pandas: add dictionaries of different length as columns

Time:03-04

given a list of dicts:

import pandas as pd
d =[{'foil': ['a', 'b', 'c']},
{'car': ['b', 'c','d','e']},
{'container': ['e','f','g','h','i','j']}]

df=pd.DataFrame()
for c in d:
    print(c)
    word   = list(c.keys())[0]
    values = c[word]

    df[word] = pd.Series(values, name=word)
df

I would like to build a dataframe where the name of every column is foil, car and container and the values are the ones of the list.

The problem when looping like above is that the result is: enter image description here

But I would like to have all the values, if needed the shorter columns might be filled with NAN (and afterwards changing the NAN to '')

Looping over every dict and adding values to the list up to the max(length) of the lists seems to me very cumbersome.

Any more pythonic way?

thanks

CodePudding user response:

You can change your list of dictionaries to a single dictionary and then call from_dict:

>>> pd.DataFrame.from_dict({k: v for dct in d for k,v in dct.items()},orient="index").T

   foil   car container
0     a     b         e
1     b     c         f
2     c     d         g
3  None     e         h
4  None  None         i
5  None  None         j

CodePudding user response:

You may check concat the result with for loop

out = pd.concat([pd.DataFrame(x) for x in d],axis=1)
Out[534]: 
  foil  car container
0    a    b         e
1    b    c         f
2    c    d         g
3  NaN    e         h
4  NaN  NaN         i
5  NaN  NaN         j

CodePudding user response:

One can use ChainMap to merge the dictionaries and the DataFrame constructor:

from collections import ChainMap
c = ChainMap(*d)
df = pd.DataFrame(c.values(), index=c).T

Or, in line with the nice answer of @not_speshal,

from collections import ChainMap
df = pd.DataFrame.from_dict(ChainMap(*d), orient="index").T

Output:

  container  car foil
0         e    b    a
1         f    c    b
2         g    d    c
3         h    e  NaN
4         i  NaN  NaN
5         j  NaN  NaN
  • Related