pandas JSON规范化,将值作为列

7kqas0il  于 2023-04-10  发布在  其他
关注(0)|答案(1)|浏览(128)

下面是JSON:

{
  "data": [
    {
      "gid": "1203715497540179",
      "completed": false,
      "custom_fields": [
        {
          "gid": "1203887422469746",
          "enabled": true,
          "name": "Inputs",
          "description": "",
          "display_value": null,
          "resource_subtype": "text",
          "resource_type": "custom_field",
          "text_value": null,
          "type": "text"
        },
        {
          "gid": "1126427465960522",
          "enabled": false,
          "name": "T-Minus",
          "description": "",
          "display_value": "54",
          "resource_subtype": "text",
          "resource_type": "custom_field",
          "text_value": "54",
          "type": "text"
        }
      ],
      "due_on": "2023-01-25",
      "name": "General Information"
    }
  ]
}

我想用它来构建下面的pandas数据框。

name                 due_on         Inputs   T-Minus
General Information  2023-01-25      null      54

我不认为仅仅通过标准化就可以做到这一点。所以我开始:

df = pd.json_normalize(test, 
                       record_path =['custom_fields'],
                       record_prefix='_',
                       errors='ignore',
                       meta=['name', 'due_on'])

这让我想到了这样一个问题:

_name _display_value name due_on .....(extra fields that I do not need)
Inputs  null         General Information
T-Minus  54          General Information

我现在如何从这个数据框转到我想要的数据框?

guicsvcw

guicsvcw1#

pd.json_normalize之后使用pivot

df = pd.json_normalize(test,  # or test['data']? 
                       record_path =['custom_fields'],
                       record_prefix='_',
                       errors='ignore',
                       meta=['name', 'due_on'])

df = (df.pivot(index=['name', 'due_on'], columns='_name', values='_display_value')
        .reset_index().rename_axis(columns=None))

输出:

>>> df
                  name      due_on Inputs T-Minus
0  General Information  2023-01-25   None      54

相关问题