Flatten Pandas DataFrame from nested json list(从嵌套的 json 列表中展平 Pandas DataFrame)
问题描述
也许有人可以帮助我.我试图将以下 ist 整合到一个熊猫数据帧中:
perhaps somebody could help me. I tried to flat the following ist into a pandas dataframe:
[{u'_id': u'2',
u'_index': u'list',
u'_score': 1.4142135,
u'_source': {u'name': u'name3'},
u'_type': u'doc'},
{u'_id': u'5',
u'_index': u'list',
u'_score': 1.4142135,
u'_source': {u'dat': u'2016-12-12', u'name': u'name2'},
u'_type': u'doc'},
{u'_id': u'1',
u'_index': u'list',
u'_score': 1.4142135,
u'_source': {u'name': u'name1'},
u'_type': u'doc'}]
结果应该是这样的:
|_id | _index | _score | name | dat | _type |
------------------------------------------------------
|1 |list |1.4142..| name1| nan | doc |
|2 |list |1.4142..| name3| nan | doc |
|3 |list |1.4142..| name1| 2016-12-12 | doc |
但我所做的一切都无法得到想要的结果.我用过这样的东西:
But all I tried to do is not possible to get the desired result. I used something like this:
df = pd.concat(map(pd.DataFrame.from_dict, res['hits']['hits']), axis=1)['_source'].T
但是后来我丢失了 _source 字段之外的类型.我也尝试使用
But then I loose the types wich is outside the _source field. I also tried to work with
test = pd.DataFrame(list)
for index, row in test.iterrows():
test.loc[index,'d'] =
但我不知道如何使用字段 _source 并将其附加到原始数据框.
But I have no idea how to come to the point to use the field _source and append it to the original data frame.
有人知道如何做到这一点并成为想要的结果吗?
Did somebody has an idea how to to that and become the desired outcome?
推荐答案
使用 json_normalize:
from pandas.io.json import json_normalize
df = json_normalize(data)
print (df)
_id _index _score _source.dat _source.name _type
0 2 list 1.414214 NaN name3 doc
1 5 list 1.414214 2016-12-12 name2 doc
2 1 list 1.414214 NaN name1 doc
这篇关于从嵌套的 json 列表中展平 Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从嵌套的 json 列表中展平 Pandas DataFrame
基础教程推荐
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
