【发布时间】:2016-12-01 06:54:20
【问题描述】:
也许有人可以帮助我。我试图将以下列表扁平化为熊猫数据框:
[{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 |
但我所做的一切都无法得到想要的结果。 我用过这样的东西:
df = pd.concat(map(pd.DataFrame.from_dict, res['hits']['hits']), axis=1)['_source'].T
但后来我失去了 _source 字段之外的类型。 我也尝试过使用
test = pd.DataFrame(list)
for index, row in test.iterrows():
test.loc[index,'d'] =
但我不知道如何使用字段 _source 并将其附加到原始数据框中。
有人知道如何做到这一点并成为理想的结果吗?
【问题讨论】:
标签: python json pandas dataframe nested