python - 由于数据长度不等,将嵌套 JSON 列表展平为 Pandas DataFrame 时出现问题

标签 python json pandas dataframe

我目前正在尝试使用以下格式的 JSON 文件:

response = {
    "leads": [{
        "id": 208827181,
        "campaignId": 2595,
        "contactId": 2919361,
        "contactAttempts": 1,
        "contactAttemptsInvalid": 0,
        "lastModifiedTime": "2017-03-14T13:37:20Z",
        "nextContactTime": "2017-03-15T14:37:20Z",
        "created": "2017-03-14T13:16:42Z",
        "updated": "2017-03-14T13:37:20Z",
        "lastContactedBy": 1271,
        "status": "automaticRedial",
        "active": True,
        "masterData": [{
                "id": 2054,
                "label": "Firmanavn",
                "value": "Firma_1"
            },
            {
                "id": 2055,
                "label": "Adresse",
                "value": "Gadenavn_1"
            },
            {
                "id": 2056,
                "label": "Postnr.",
                "value": "2000"
            },
            {
                "id": 2057,
                "label": "Bydel",
                "value": "Frederiksberg"
            },
            {
                "id": 2058,
                "label": "Telefonnummer",
                "value": "25252525"
            }
        ]
    }]
}

masterData 采用嵌套列表格式,但长度也有所不同。基本上,每行/条目都可以分配不同的列。我希望为每个条目保留一个或多个特定列。然而,在我当前的索引中,由于嵌套列表的长度不同,我的索引会中断。 这是我的代码:

leads = json_normalize(response['leads'])
df = pd.concat([leads.drop('masterData', 1),
            pd.DataFrame(list(pd.DataFrame(list(leads['masterData']))[4]))
                        .drop(['id', 'label'], 1)
                        .rename(columns={"value": "tlf"})], axis=1)

所需的输出是:

    active   campaignId  contactAttempts    contactAttemptsInvalid  contactId           created               id          lastContactedBy   lastModifiedTime        nextContactTime       resultData    status          updated               tlf
0   True       2595        1                  0                       2919361             2017-03-14T13:16:42Z  208827181   1271.0          2017-03-14T13:37:20Z    2017-03-15T14:37:20Z    []          automaticRedial 2017-03-14T13:37:20Z    37373737
1   True       2595        2                  0                       2919359             2017-03-14T13:16:42Z  208827179   1271.0          2017-03-14T13:33:30Z    2017-03-15T14:33:30Z    []          privateRedial   2017-03-14T13:33:30Z    55555555
2   True       2595        1                  0                       2919360             2017-03-14T13:16:42Z  208827180   1271.0          2017-03-14T13:36:06Z    None                    []          success         2017-03-14T13:36:06Z    22222222
3   True       2595        1                  0                       2919362             2017-03-14T13:16:42Z  208827182   1271.0          2017-03-14T13:56:39Z    None                    []          success         2017-03-14T13:56:39Z    34343434

其中“tlf”是从“masterData”添加的列。

最佳答案

仅使用 json_normalize 并在列表中指定列名称:

L = ['active', 'campaignId', 'contactAttempts', 'contactAttemptsInvalid', 
     'contactId', 'created', 'id', 'lastContactedBy', 'lastModifiedTime', 
     'nextContactTime', 'status', 'updated']
df = json_normalize(response['leads'], 'masterData', L, record_prefix='masterData.')

print (df)
   masterData.id masterData.label masterData.value  active  campaignId  \
0           2054        Firmanavn          Firma_1    True        2595   
1           2055          Adresse       Gadenavn_1    True        2595   
2           2056          Postnr.             2000    True        2595   
3           2057            Bydel    Frederiksberg    True        2595   
4           2058    Telefonnummer         25252525    True        2595   

   contactAttempts  contactAttemptsInvalid  contactId               created  \
0                1                       0    2919361  2017-03-14T13:16:42Z   
1                1                       0    2919361  2017-03-14T13:16:42Z   
2                1                       0    2919361  2017-03-14T13:16:42Z   
3                1                       0    2919361  2017-03-14T13:16:42Z   
4                1                       0    2919361  2017-03-14T13:16:42Z   

          id  lastContactedBy      lastModifiedTime       nextContactTime  \
0  208827181             1271  2017-03-14T13:37:20Z  2017-03-15T14:37:20Z   
1  208827181             1271  2017-03-14T13:37:20Z  2017-03-15T14:37:20Z   
2  208827181             1271  2017-03-14T13:37:20Z  2017-03-15T14:37:20Z   
3  208827181             1271  2017-03-14T13:37:20Z  2017-03-15T14:37:20Z   
4  208827181             1271  2017-03-14T13:37:20Z  2017-03-15T14:37:20Z   

            status               updated  
0  automaticRedial  2017-03-14T13:37:20Z  
1  automaticRedial  2017-03-14T13:37:20Z  
2  automaticRedial  2017-03-14T13:37:20Z  
3  automaticRedial  2017-03-14T13:37:20Z  
4  automaticRedial  2017-03-14T13:37:20Z  

关于python - 由于数据长度不等,将嵌套 JSON 列表展平为 Pandas DataFrame 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52964448/

相关文章:

ios - 如何使用 JSONModel 获取 NSArray 对象

python - Pandas 优化插值/计数算法

python - 在 python pandas 中迭代多索引数据

python - Nginx + Gunicorn - 静态资源错误页面

python函数在 Pyramid 中的行为不同?

python - X 和 y 具有不兼容的形状

json - 如何使用代理接口(interface)在 RestEasy 客户端框架中设置 "Content-type" header ?

javascript - 使用 JavaScript 的 md5 的较短版本..?

python - 如何将 pandas DataFrame 表保存为 png

python - Flask:OSError:[Errno 98]地址已在使用中 - 但为什么?