在 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
Extract nested JSON in pandas dataframe
提问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_normalize
but 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
- Use
json_normalize
to handle alist
of 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.
- 使用
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
- Pivot the
DF
with an optionalaggfunc
to handle duplicated index axis:
DF
使用一个可选的枢轴aggfunc
来处理重复的索引轴:
DF = df_info.pivot_table(index=df_info.index.get_level_values(1), columns=['b'],
values=['a'], aggfunc=' '.join)
DF
- Finally Concatenate sideways:
- 最后横向连接:
pd.concat([df[['ID']], DF.xs('a', axis=1).rename_axis(None, 1)], axis=1)
Starting DF
used:
开始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'}]]))