2

I would like to transform a DataFrame looking like this:

         dictionary
0  {'b': 2, 'a': 1}
1  {'c': 4, 'b': 3}

from

import pandas
df = pandas.DataFrame()
df['dictionary'] = [{'a':1,'b':2},{'b': 3,'c':4}]

onto a DataFrame looking like this:

         dictionary    a  b    c
0  {'b': 2, 'a': 1}  1.0  2  NaN
1  {'c': 4, 'b': 3}  NaN  3  4.0

where (of course) the order of the columns does not matter.

How can I do this without explicitly looping through the dictionaries or the rows ?

Cedric H.
  • 7,980
  • 10
  • 55
  • 82

2 Answers2

4

A vectorized approach by converting the given series to it's list representation and then performing concatenation column-wise:

pd.concat([df['dictionary'], pd.DataFrame(df['dictionary'].values.tolist())], axis=1)

enter image description here

Nickil Maveli
  • 29,155
  • 8
  • 82
  • 85
3

You can apply a pd.Series transformation and concat the two:

pd.concat([df, df['dictionary'].apply(pd.Series)], axis=1)
Out: 
         dictionary    a    b    c
0  {'b': 2, 'a': 1}  1.0  2.0  NaN
1  {'b': 3, 'c': 4}  NaN  3.0  4.0

Or you could use join

In [4]: df.join(df.dictionary.apply(pd.Series))
Out[4]:
           dictionary    a    b    c
0  {u'a': 1, u'b': 2}  1.0  2.0  NaN
1  {u'c': 4, u'b': 3}  NaN  3.0  4.0
Zero
  • 74,117
  • 18
  • 147
  • 154
ayhan
  • 70,170
  • 20
  • 182
  • 203