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

后端 未结 1 1842
梦如初夏
梦如初夏 2021-01-31 11:27

TL;DR If loaded fields in a Pandas DataFrame contain JSON documents themselves, how can they be worked with in a Pandas like fashion?

Currently I\'m dir

相关标签:
1条回答
  • 2021-01-31 12:20

    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]: df
    Out[2]: 
       0                   1
    0  1           {u'a': 2}
    1  2  {u'a': 1, u'b': 3}
    
    In [3]: df[1].apply(pd.Series)
    Out[3]: 
       a   b
    0  2 NaN
    1  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 name
    
    In [5]: pd.concat([df, dict_col.apply(pd.Series)], axis=1)
    Out[5]: 
       0  a   b
    0  1  2 NaN
    1  2  1   3
    

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

    0 讨论(0)
提交回复
热议问题