从嵌套的json列表中展平Pandas DataFrame [英] Flatten Pandas DataFrame from nested json list

查看:147
本文介绍了从嵌套的json列表中展平Pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人可以帮助我.我试图将以下ist放到pandas数据框中:

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?

推荐答案

使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆