2

我所拥有的是来自 monday.com 通过 graphql 查询的结果。数据返回许多不同的集合,例如字典列表的字典......例如。

{
  "data": {
    "boards": [
      {
        "name": "board name",
        "groups": [
                    {"title": "group title 1"},
                    {"title": "group title 2"},
                    {"title": "group title 3"},
                    {"title": "group title 4"}
                  ]
       }
     ]
   },
  "account_id": "0000000"
}

dataframe我想做一只熊猫

     group.board    group.title
0    'board_name'   'group title 1'
1    'board_name'   'group title 2'
2    'board_name'   'group title 3'
3    'board_name'   'group title 4'

我试过了

pd.json_normalize(json_data, 'boards')

但我不断收到 KeyError: 'boards'

4

2 回答 2

0

您的 json 字典无效(缺少右括号,所以我修复了它)。我会这样做。在这里,我们将进入 dict,json_data["data"]["boards"]因为这是数据所在的位置,"groups"用作records键,并将字段"name"用作元数据字段之一。

import pandas as pd

json_data = {
    "data": {
        "boards": [
            {
                "name": "board name",
                "groups": [
                    { "title": "group title 1" },
                    { "title": "group title 2" },
                    { "title": "group title 3" },
                    { "title": "group title 4" }
                ]
            },
        ]
    },
    "account_id": "0000000"
}

pd.json_normalize(json_data["data"]["boards"], "groups", ["name"])

输出:


    title           name
0   group title 1   board name
1   group title 2   board name
2   group title 3   board name
3   group title 4   board name
于 2021-10-25T21:06:15.480 回答
0

你得到一个 keyerror 因为json_datahas no key boardsjson_data["data"]确实如此,但您仍然无法获得预期的结果。

您需要将 passjson_data["data"]["boards"]作为 dicts 列表,将“组”作为record_path,并["name"]作为meta路径:

>>> df = pd.json_normalize(json_data["data"]["boards"], "groups", ["name"])
           title        name
0  group title 1  board name
1  group title 2  board name
2  group title 3  board name
3  group title 4  board name

然后,您可以重命名列:

>>> df.columns = ["group.title", "group.board"]

     group.title group.board
0  group title 1  board name
1  group title 2  board name
2  group title 3  board name
3  group title 4  board name
于 2021-10-25T21:06:26.183 回答