【问题标题】:json_normalize: Accessing data from an object in an arrayjson_normalize:从数组中的对象访问数据
【发布时间】:2020-11-24 15:52:30
【问题描述】:

我需要从 JSON“列表”中提取大量信息到 pandas 数据帧。

考虑以下 JSON 文件:

{
    "List": [
        {
            "Name": "xml",
            "CreateTime": "2020-11-13T10:44:20",
            "Descriptor": {
                "Location": "some/url",
                ...
            }
            "Permissions": [
                {
                    "Principal": {
                        "Identifier": "ALLOWED_PRINCIPALS"
                    },
                    "Example": [
                        "ALL"
                    ]
                }
            ],
        },
        ...
    ]
}

我正在使用以下 python 代码从此 json 中提取信息到 pandas 数据帧:

for List in response['List']:
    df = pd.json_normalize(response['List'])
    df = df.reindex(columns=['Name','CreateTime','Descriptor.Location','Permissions[Identifier]'])

前三列的填充没有问题,但我无法访问存储在 a 中的数据

  • 库({} 括号):例如“Permissions[Identifier]”和
  • 数组([] 括号):例如“Permissions[Example]”。

其他列填充数据框,而我只为标识符和示例获取 NULL。 我也尝试过指定:Permissions['Identifier'],但在我的示例中这将是一个语法错误。

第二个例子:嵌套库中的数组

{
    "Table": [
        {
            "TableName": "data",
            "Descriptor": {
                "Columns": [
                    {
                        "Name": "category",
                        "Type": "string"
                    },
                    {
                        "Name": "author",
                        "Type": "string"
                    },
                    {
                        "Name": "title",
                        "Type": "string"
                    },
                    ...

在这种情况下,我试图访问“描述符”->“列”下的多个“名称”列的值

在第一部分使用 Jonathan Leon 的解决方案,我在第二个示例中尝试了以下代码:

for DatabaseList in db_response['Table']:
    df = pd.json_normalize(db_response['Table'], record_path=['Descriptor.Columns'], 
                        meta=['TableName', ['Descriptor.Columns']])
    df = df.reindex(columns=['TableName','Name'])
    

不幸的是,这样定义记录路径是不可能的。我必须以某种方式定义一个嵌套的记录路径来访问“列”中的数据。

我还必须将 record_prefix 添加到 json_normalize 部分,因为有多个“名称”列。

在我的示例中,使用 python 代码访问库和数组数据的正确语法是什么?

【问题讨论】:

    标签: pandas


    【解决方案1】:

    根据您提供的截断列表,这应该可以帮助您入门。 json_normalize 的文档是here。对于多层列表,您可能需要循环和连接数据帧。

    df = pd.json_normalize(response['List'], record_path=['Permissions'], meta=['Name', 'CreateTime', ['Descriptor', 'Location']],
                            meta_prefix='', record_prefix='')
    df = df.reindex(columns=['Name','CreateTime','Descriptor.Location','Principal.Identifier', 'Example'])
    df.rename(columns={'Principal.Identifier':'Permissions.Identifier'})
    pd.concat([ df, df['Example'].apply(pd.Series)], axis = 1)
    

    输出(我修改了示例列表以说明扩展值):

      Name           CreateTime Descriptor.Location Principal.Identifier      Example    0     1
    0  xml  2020-11-13T10:44:20            some/url   ALLOWED_PRINCIPALS  [ALL, SOME]  ALL  SOME
    

    【讨论】:

    • 是的,它适用于我提到的两种情况。非常感谢您花时间为我的示例编写代码。
    • 您介意再看一个我遇到问题的案例吗?
    • @ire 很高兴它对你有用。用您尝试过的方法发布您的其他问题。最好让多人有机会回答。
    • 我已将第二部分添加到我的主帖中。
    【解决方案2】:

    对于第二个示例,这应该可以帮助您入门

    response = {
        "Table": [
            {
                "TableName": "data",
                "Descriptor": {
                    "Columns": [
                        {
                            "Name": "category",
                            "Type": "string"
                        },
                        {
                            "Name": "author",
                            "Type": "string"
                        },
                        {
                            "Name": "title",
                            "Type": "string"
                        }]}}]}
    
    cols = response['Table'][0]['Descriptor']['Columns']
    df = pd.DataFrame(cols)
    df.drop('Type', axis=1, inplace=True)
    df['TableName'] = response['Table'][0]['TableName']
    

    【讨论】:

    • 它确实让我开始了,我想出了如何获得我想要的数据 :) 非常感谢
    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多