【问题标题】:Multiple Nested Dictionaries with Pandas带有 Pandas 的多个嵌套字典
【发布时间】:2020-12-20 21:11:01
【问题描述】:

有没有办法将这种 JSON 响应导入 Pandas?我一直试图让它与 json_normalize 一起使用,但我似乎一次无法获得超过一个级别的工作(我可以得到笔记但不能在 custom_fields 中)。我也无法弄清楚如何调用 ['reporter']['name'] 之类的东西(应该是 jdoe)。这是来自 Mantis 及其请求响应的 JSON 输出。我现在想知道是否需要将其分解为多个帧并重新组合在一起,还是应该使用 for 循环并将我想要的数据放入更好的格式以供 PD 导入?

在我的脑海中,每个项目都应该是系列中的一列,都像这样与 id 列相关联。

id | summary | project.name | reporter.name ..|.. custom.fields.Project_Stage | ... notes1.reporter.name | notes1.text ... notes2.reporter.name | notes2.text



{
  "issues": [
    {
      "id": 1234,
      "summary": "Some text",
      "project": {
        "id": 1,
        "name": "North America"
      },
      "category": {
        "id": 11,
        "name": "Retail"
      },
      "reporter": {
        "id": 1099,
        "name": "jdoe"
      },
      "custom_fields": [
        {
          "field": {
            "id": 107,
            "name": "Product Escalations"
          },
          "value": ""
        },
        {
          "field": {
            "id": 1,
            "name": "Project_Stage"
          },
          "value": "Pending"
        }
      ],
      "notes": [
        {
          "id": 214288,
          "reporter": {
            "id": 9999,
            "name": "jdoe"
          },
          "text": "Worked with Mark over e-mail",
          "view_state": {
            "id": 10,
            "name": "public",
            "label": "public"
          },
          "type": "note",
          "created_at": "2020-12-04T15:55:02-08:00",
          "updated_at": "2020-12-04T15:55:02-08:00"
        },
        {
          "id": 214289,
          "reporter": {
            "id": 9999,
            "name": "jdoe"
          },
          "text": "I attempted on numerous occasions to setup a meeting with him to set it up for him.",
          "view_state": {
            "id": 10,
            "name": "public",
            "label": "public"
          },
          "type": "note",
          "created_at": "2020-12-04T15:57:02-08:00",
          "updated_at": "2020-12-04T15:57:02-08:00"
        }
      ]
    }
  ]
}

这就是我脑海中 DF 的样子。一条线/系列一张票的所有数据。

【问题讨论】:

  • 请发布您预期输出的数据框
  • 最好的方法是@sammywemmy?
  • 创建一个表格,可能是excel或记事本,其中包含每行的标题和值。复制并粘贴到您的问题中。它将指导 SO 人适当地回答您的问题,并消除猜测

标签: python python-3.x pandas dataframe


【解决方案1】:

那些在同一级别有许多列表的结构很棘手。试试 flatten_json。 https://github.com/amirziai/flatten

如果你的响应被称为'dic',你可以使用这个。

from flatten_json import flatten
dic_flattened = (flatten(d, '.') for d in dic['issues'])
df = pd.DataFrame(dic_flattened)

输出

     id    summary  project.id   project.name  category.id category.name  ...  notes.1.view_state.id notes.1.view_state.name  notes.1.view_state.label notes.1.type         notes.1.created_at         notes.1.updated_at
0  1234  Some text           1  North America           11        Retail  ...                     10                  public                    public         note  2020-12-04T15:57:02-08:00  2020-12-04T15:57:02-08:00

In [101]: df.columns
Out[101]:
Index(['id', 'summary', 'project.id', 'project.name', 'category.id',
       'category.name', 'reporter.id', 'reporter.name',
       'custom_fields.0.field.id', 'custom_fields.0.field.name',
       'custom_fields.0.value', 'custom_fields.1.field.id',
       'custom_fields.1.field.name', 'custom_fields.1.value', 'notes.0.id',
       'notes.0.reporter.id', 'notes.0.reporter.name', 'notes.0.text',
       'notes.0.view_state.id', 'notes.0.view_state.name',
       'notes.0.view_state.label', 'notes.0.type', 'notes.0.created_at',
       'notes.0.updated_at', 'notes.1.id', 'notes.1.reporter.id',
       'notes.1.reporter.name', 'notes.1.text', 'notes.1.view_state.id',
       'notes.1.view_state.name', 'notes.1.view_state.label', 'notes.1.type',
       'notes.1.created_at', 'notes.1.updated_at'],
      dtype='object')

【讨论】:

    猜你喜欢
    • 2020-02-18
    • 2018-03-14
    • 2023-04-02
    • 2018-01-29
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多