How do I access embedded json objects in a Pandas DataFrame? How do I access embedded json objects in a Pandas DataFrame? pandas pandas

How do I access embedded json objects in a Pandas DataFrame?


One solution is just to smash it with the Series constructor:

In [1]: df = pd.DataFrame([[1, {'a': 2}], [2, {'a': 1, 'b': 3}]])In [2]: dfOut[2]:    0                   10  1           {u'a': 2}1  2  {u'a': 1, u'b': 3}In [3]: df[1].apply(pd.Series)Out[3]:    a   b0  2 NaN1  1   3

In some cases you'll want to concat this to the DataFrame in place of the dict row:

In [4]: dict_col = df.pop(1)  # here 1 is the column nameIn [5]: pd.concat([df, dict_col.apply(pd.Series)], axis=1)Out[5]:    0  a   b0  1  2 NaN1  2  1   3

If the it goes deeper, you can do this a few times...