【问题标题】:How to list JSON non-list items together with list items with pandas.json_normalize with Python?python - 如何使用Python列出JSON非列表项以及带有pandas.json_normalize的列表项?
【发布时间】:2021-07-12 08:59:06
【问题描述】:
[{
    "builtin_name": "custom_template",
    "fields": [{
            "id": 10012,
            "field_type": "OBJECT_SET",
            "tooltip_text": "",
            "name_plural": "",
            "name_singular": "reference",
            "backref_name": "reference",
            "backref_tooltip_text": "",
            "allow_multiple": False,
            "allowed_otypes": [
                "schema",
                "table",
                "attribute",
                "user",
                "groupprofile",
                "groupprofile"
            ],
            "options": None,
            "builtin_name": None
        }, {
            "id": 8,
            "field_type": "OBJECT_SET",
            "tooltip_text": None,
            "name_plural": "Stewards",
            "name_singular": "Steward",
            "backref_name": "Steward",
            "backref_tooltip_text": None,
            "allow_multiple": True,
            "allowed_otypes": [
                "user",
                "groupprofile",
                "groupprofile"
            ],
            "options": None,
            "builtin_name": "steward"
        }
    ],
    "id": 16,
    "title": "Custom template"
}]

使用这个 JSON 对象,我想使用 pandas.json_normalize 对其进行规范化。

当我这样做时:

pd.json_normalize(data, "fields", errors='ignore', record_prefix='')

我得到fields 以漂亮的表格形式列出,如下所示:

id field_type tooltip_text name_plural name_singular backref_name backref_tooltip_text allow_multiple allowed_otypes options builtin_name

(后跟数据行)

但我也是外部属性,id、title 和 builtin_name 与字段一起列出

这样我就结束了:

id builtin_name title id field_type tooltip_text name_plural name_singular backref_name backref_tooltip_text allow_multiple allowed_otypes options builtin_name

我试过这个:

pd.json_normalize(data, ["id", "builtin_name", "title"], "fields", errors='ignore', record_prefix='')

但它会抛出一个错误,说 id 不是列表。

也试过不带方括号也无济于事。

如何让这些字段"id", "builtin_name", "title" 与每一行中的其他字段一起列出?

谢谢!

【问题讨论】:

    标签: python pandas json-normalize


    【解决方案1】:

    我会在整个data 列表中使用.json_normalize 和.explode() fields 列。然后concat回来获取想要的DataFrame:

    df = pd.json_normalize(data, errors="ignore", record_prefix="")
    df = pd.concat(
        [df, df.explode("fields")["fields"].apply(pd.Series)], axis=1
    ).drop(columns="fields")
    print(df)
    

    打印:

          builtin_name  id            title     id  field_type tooltip_text name_plural name_singular backref_name backref_tooltip_text  allow_multiple                                     allowed_otypes options builtin_name
    0  custom_template  16  Custom template  10012  OBJECT_SET                              reference    reference                                False  [schema, table, attribute, user, groupprofile,...    None         None
    0  custom_template  16  Custom template      8  OBJECT_SET         None    Stewards       Steward      Steward                 None            True                 [user, groupprofile, groupprofile]    None      steward
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 2013-04-04
      • 2022-11-28
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      相关资源
      最近更新 更多