Pandas Dataframe Transformation: Adding Columns From Dictionary K-v Pairs
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'] =
Solution 1:
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)
Solution 2:
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.02.0  NaN
1  {u'c': 4, u'b': 3}  NaN  3.04.0
Post a Comment for "Pandas Dataframe Transformation: Adding Columns From Dictionary K-v Pairs"