将嵌套的 json 转换为具有特定输出的数据帧

时间:2021-06-10 12:43:13

标签: python json pandas data-science

假设我有一些 JSON 如下

response = {
        "totalrecords": 2,
        "data": [
            {
                "stateCd": "U.K",
                "stateName": "uttarakhand",
                "details": {
                    "id": [
                        "2312-k",
                        "2312-k"
                    ],
                    "date": [
                        "10-OCT-2019",
                        "11-OCT-2019"
                    ],
                    "icp": [
                        2233,
                        6443
                    ],
                    "icpr": [
                        3.434,
                        23.232
                    ]
                }
            },
            {
                "stateCd": "U.P",
                "stateName": "uttar pradesh",
                "details": {
                    "id": [
                        "2712-k",
                        "5412-k"
                    ],
                    "date": [
                        "10-OCT-2019",
                        "11-OCT-2019"
                    ],
                    "icp": [
                        2233,
                        6443
                    ],
                    "icpr": [
                        32.434,
                        31.232
                    ]
                }
            }
        ]
    }

我想将其转换为如下数据框

enter image description here

但是在尝试使用 pandas.json_normalize() 将其转换为数据帧时 我无法达到我想要的输出

我尝试过的:

data_trunc=response['data'] # to extract data from response
pd.json_normalize(data_trunc)

enter image description here

pd.json_normalize(data_trunc,record_path=['details','id'],meta=['stateCd','stateName'])

enter image description here

但这不包括 dateicpicpr

所以我尝试了不同的排列组合

    pd.json_normalize(data_trunc,record_path=[['details','id'],['date']],meta=['stateCd','stateName'])

pd.json_normalize(data_trunc,record_path=[['details','id'],['details'.'date']],meta=['stateCd','stateName'])

但遇到同样的错误 TypeError: unhashable type: 'list'

1 个答案:

答案 0 :(得分:2)

你需要爆炸。

pd.json_normalize(data_trunc).apply(pd.Series.explode)

<头>
stateCd stateName details.id details.date details.icp details.icpr
0 英国 北阿坎德邦 2312-k 10-OCT-2019 2233 3.434
0 英国 北阿坎德邦 2312-k 11-OCT-2019 6443 23.232
1 U.P 北方邦 2712-k 10-OCT-2019 2233 32.434
1 U.P 北方邦 5412-k 11-OCT-2019 6443 31.232
相关问题