0

I have data in the form of a pandas dataframe, which looks like this:

A      B                               C
ab     {"app":"x","wid":10,"a42":5}    e
y      {"hair":"x","wid":10,"a42":2}   r

and I want to convert my dataframe like this:

A    C    app    wid    a42    hair
ab   e    x      10     5      -
y    r    -      10     2      r

These questions were not helpful:
JSON to pandas DataFrame
Convert Json data to Python DataFrame
How to convert a parsed json file to pandas data frame?

And I have also seen many other links but stuck with this problem.

Community
  • 1
  • 1
  • Duplicate? http://stackoverflow.com/questions/38231591/splitting-dictionary-list-inside-a-pandas-column-into-separate-columns/38233518#38233518 – Merlin Jul 07 '16 at 12:23

3 Answers3

1

try this:

In [143]: df.B.apply(pd.Series)
Out[143]:
   a42  app hair  wid
0    5    x  NaN   10
1    2  NaN    x   10
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
1

You can use DataFrame.from_records and concat:

import pandas as pd

df = pd.DataFrame({'A':['ab','y'],
                   'B':[{"app":"x","wid":10,"a42":5},{"hair":"x","wid":10,"a42":2}],
                   'C':['e','r']})
print (df)
    A                                   B  C
0  ab   {'a42': 5, 'wid': 10, 'app': 'x'}  e
1   y  {'a42': 2, 'wid': 10, 'hair': 'x'}  r

print (pd.DataFrame.from_records(df.B))
   a42  app hair  wid
0    5    x  NaN   10
1    2  NaN    x   10

print (pd.concat([df[['A','C']], pd.DataFrame.from_records(df.B)], axis=1))
    A  C  a42  app hair  wid
0  ab  e    5    x  NaN   10
1   y  r    2  NaN    x   10
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I don't have data in this form : {'A':['ab','y'], 'B':[{"app":"x","wid":10,"a42":5},{"hair":"x","wid":10,"a42":2}], 'C':['e','r']}. I have it in the form of dataframe and when I am doing the process "pd.DataFrame.from_records(df.B)" result comes wrong. – Abhishek Shankhadhar Jul 07 '16 at 11:44
  • yes, I know. You have data in form `df`, right? I add creating df for better testing. – jezrael Jul 07 '16 at 11:55
1

Try this;

df["B"] = df["B"].apply(lambda x : dict(eval(x)) )
or 
df["B"] = df["B"].map(eval)

df2 = df["B"].apply(pd.Series )
result = pd.concat([df, df2], axis=1).drop('B', axis=1)
Merlin
  • 24,552
  • 41
  • 131
  • 206