从python中嵌套的json数组中提取数据 [英] Extracting data from nested json arrays in python

查看:528
本文介绍了从python中嵌套的json数组中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从 Python 中的嵌套 json 中提取数据.我想创建一个包含bill"所有值的一列熊猫数据框,例如

I am having trouble extracting data from nested json in python. I want to create a one column pandas dataframe of all the values of "bill", e.g.

bill
----
a1
a2
a3

使用格式如下的 API 输出:

Using the output from an API formatted like this:

{
  "status": "succeeded",
  "travels": [
    {
      "jobs": [
        {
          "bill": "a1"
        },
        {
          "bill": "a2"
        },
        {
          "bill": "a3"
        }
      ],
      "vehicle": {
        "plate": "xyz123"
      }
    }
  ]
}

将json 直接加载到pandas 中只给了我'bill' 的第一个实例.我在jobs"上尝试过 json_normalize(),但它有一个关键错误.谁能帮我弄清楚如何只拿到账单"?

Loading the json directly into pandas gives me only the first instance of 'bill'. I have tried json_normalize() on 'jobs', but it has a key error. Can anybody help me figure out how to grab just the 'bill'?

谢谢

推荐答案

我认为您在 json_normalize 方面走在正确的轨道上.将您的输入作为 python 字典 d:

I think you were on the right track with json_normalize. With your input as a python dictionary d:

from pandas.io.json import json_normalize
json_normalize(d, record_path=['travels', 'jobs'])
  bill
0   a1
1   a2
2   a3

这篇关于从python中嵌套的json数组中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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