【问题标题】:Expand column dataframe nested dictionary展开列数据框嵌套字典
【发布时间】:2021-04-18 10:32:05
【问题描述】:

我做了一些尝试,但未能达到目标。 我完全确定这是一个容易有人马上解决的问题。 我正在尝试将 json 输出解析为数据框,但我被困在嵌套字典中。 给定这个例子:

{'queryResponse': {'@count': 9364,
               '@domain': 'ROOT-DOMAIN',
               '@first': 0,
               '@last': 4,
               '@requestUrl': 'https://10.71.167.210/webacs/api/v4/data/Devices?.full=true&.maxResults=5',
               '@responseType': 'listEntityInstances',
               '@rootUrl': 'https://10.71.167.210/webacs/api/v4/data',
               '@type': 'Devices',
               'entity': [{'@dtoType': 'devicesDTO',
                           '@type': 'Devices',
                           '@url': 'https://10.71.167.210/webacs/api/v4/data/Devices/3795793',
                           'devicesDTO': {'@displayName': '3795793',
                                          '@id': 3795793,
                                          'adminStatus': 'MANAGED',
                                          'collectionDetail': '<status><general '
                                                              'code="SUCCESS"/></status>',
                                          'collectionStatus': 'COMPLETED',
                                          'collectionTime': '2021-01-12T21:43:02.337Z',
                                          'creationTime': '2016-05-05T10:18:39.182Z',
                                          'deviceId': 3984007,
                                          'deviceName': 'AP-02Expressdamiantellez.carrefour.es',
                                          'deviceType': 'Cisco Aironet '
                                                        '1210 Access Point',
                                          'ipAddress': '10.217.36.112',
                                          'location': 'AP-02',
                                          'managementStatus': 'MANAGED_AND_SYNCHRONIZED',
                                          'manufacturerPartNrs': {'manufacturerPartNr': [{'partNumber': 'AIR-AP1231G-E-K9    '}]},
                                          'productFamily': 'Autonomous AP',
                                          'reachability': 'REACHABLE',
                                          'softwareType': 'IOS',
                                          'softwareVersion': '12.3(8)JEE'}},

我已经能够解析到这个数据框: df_inventory

我需要将“PartNumber”(或新列)修改为包含的确切部件号(例如“WS-C2950C-24”)。 这是我的尝试:

df['newPartNumber'] = df['manufacturerPartNrs']['manufacturerPartNr'][0]['partNumber'].strip()

我得到一个 KeyError: 'manufacturerPartNr' 试图这样做。

这是df:

{'deviceName': {0: 'AP-02Expressdamiantellez.carrefour.es', 1: 'AP-01expressastorga.carrefour.es', 2: 'SW-Viajes-1.elche', 3: 'SW-Express-116-1.expresspuentearce', 4: 'Z-Win2.es.wcorp.carrefour.com'}, 'deviceType': {0: 'Cisco Aironet 1 210 Access Point', 1: 'Cisco Aironet 1210 Access Point', 2: 'Cisco Catalyst 2950C 24 Switch', 3: 'Cisco Catalyst 2960-24-S Switch', 4: 'Cisco Catalyst 4506 Switch'}, 'ipAddress': {0: '10.217.36.112', 1: '10.209.221.111', 2: '10.68.87.145', 3: '10.209.116.125', 4: '10.71.79.52'}, 'softwareVersion': {0: '12.3(8)JEE', 1: '12.3(8)JEE', 2: '12.1(22)EA14', 3: '15.0(2)SE10a', 4: '12.2(53)SG1'}, 'PartNumber': {0: "{'manufacturerPartNr': [{'partNumber': 'AIR-AP1231G-E-K9'}]}", 1: "{'manufacturerPartNr': [{'partNumber': 'AIR-AP1231G-E-K9  '}]}", 2: "{'manufacturerPartNr': [{'partNumber': 'WS-C2950C-24'}]}", 3: "{'manufacturerPartNr': [{'partNumber': 'WS-C2960-24-S'}]}", 4: "{'manufacturerPartNr': [{'partNumber': 'WS-C4506'}]}"}}

有人可以帮我完成这最后一步吗?

【问题讨论】:

    标签: python pandas dataframe dictionary nested


    【解决方案1】:

    我可以解决这个问题。 如果有人觉得它有用,我会发布答案。

    df["NewPart"] = [i['manufacturerPartNr'][0]['partNumber'].strip() for i in df['manufacturerPartNrs']]
    

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      • 2022-12-05
      • 2020-09-06
      • 2021-06-11
      相关资源
      最近更新 更多