在 Pandas 数据框中提取嵌套的 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40702633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:28:03  来源:igfitidea点击:

Extract nested JSON in pandas dataframe

pythonjsonpandas

提问by Nickil Maveli

I am trying to unpack nested JSON in the following pandas dataframe:

我正在尝试在以下 Pandas 数据帧中解压嵌套的 JSON:

           id                                                              info
0           0  [{u'a': u'good', u'b': u'type1'}, {u'a': u'bad', u'b': u'type2'}]
1           1  [{u'a': u'bad', u'b': u'type1'}, {u'a': u'bad', u'b': u'type2'}]
2           2  [{u'a': u'good', u'b': u'type1'}, {u'a': u'good', u'b': u'type2'}]

My expected outcome is:

我的预期结果是:

           id        type1    type2
0           0        good     bad
1           1        bad      bad
2           2        good     good

I've been looking at other solutions including json_normalizebut it does not work for me unfortunately. Should I treat the JSON as a string to get what I want? Or is there a more straight forward way to do this?

我一直在寻找其他解决方案,包括json_normalize但不幸的是它对我不起作用。我应该将 JSON 视为字符串以获得我想要的吗?或者有没有更直接的方法来做到这一点?

回答by Nickil Maveli

  1. Use json_normalizeto handle a listof dictionaries and break individual dicts into separate series after setting the common path, which is infohere. Then, unstack+ apply series which gets appended downwards for that level.
  1. 使用json_normalize来处理list字典和设置共同的路径,这是突破后的个别类型的字典成独立的系列信息在这里。然后,unstack+ apply 系列向下附加到该级别。


from pandas.io.json import json_normalize

df_info = json_normalize(df.to_dict('list'), ['info']).unstack().apply(pd.Series)
df_info

enter image description here

在此处输入图片说明

  1. Pivot the DFwith an optional aggfuncto handle duplicated index axis:
  1. DF使用一个可选的枢轴aggfunc来处理重复的索引轴:


DF = df_info.pivot_table(index=df_info.index.get_level_values(1), columns=['b'], 
                         values=['a'], aggfunc=' '.join)

DF

enter image description here

在此处输入图片说明

  1. Finally Concatenate sideways:
  1. 最后横向连接:


pd.concat([df[['ID']], DF.xs('a', axis=1).rename_axis(None, 1)], axis=1)

enter image description here

在此处输入图片说明



Starting DFused:

开始DF使用:

df = pd.DataFrame(dict(ID=[0,1,2], info=[[{u'a': u'good', u'b': u'type1'}, {u'a': u'bad', u'b': u'type2'}], 
                                        [{u'a': u'bad', u'b': u'type1'}, {u'a': u'bad', u'b': u'type2'}],
                                        [{u'a': u'good', u'b': u'type1'}, {u'a': u'good', u'b': u'type2'}]]))