[英]JSON_Normalize Pandas with lists
我有一個按以下嵌套方式格式化的 JSON 文件。
[
{
"unitCode": "ABCD",
"bedType": "Adult MT/MS",
"census": 13,
"subCensus": null,
"censusDetails": [],
"occupancy": 62,
"occupancyStar": null,
"occupancyAlertStatus": null,
"columns": [
{
"id": "blockedBeds",
"value": "1",
"hoverDetails": [
{
"id": "bedName",
"value": "23_1"
}
]
},
{
"id": "unOccupied",
"value": "2",
"hoverDetails": [
{
"id": "bedName",
"value": "20a_2"
},
{
"id": "bedName",
"value": "22a_1"
}
]
}
],
"codeEvents": null,
"codeEventDetails": null
},
{
"unitCode": "EFGH",
"bedType": "Adult MT/MS",
"census": 14,
"subCensus": null,
"censusDetails": [],
"occupancy": 61,
"occupancyStar": null,
"occupancyAlertStatus": null,
"columns": [
{
"id": "blockedBeds",
"value": "1",
"hoverDetails": [
{
"id": "bedName",
"value": "52_2"
}
]
},
{
"id": "unOccupied",
"value": "1",
"hoverDetails": [
{
"id": "bedName",
"value": "53_1"
}
]
}
],
"codeEvents": null,
"codeEventDetails": null
}
]
我正在嘗試展平此文件並將其轉換為帶有 json_normalize 的json_normalize
。 這是我的代碼: testhover = json_normalize(data, ['columns'],['unitCode'])
我得到的 dataframe 如下所示:
id | value | hoverDetails | unitCode
0 blockedBeds | 1 | [{'id': 'bedName', 'value': '23_1'}] | ABCD
1 unOccupied | 2 | [{'id': 'bedName', 'value': '20a_2'}, {'id': '...' | ABCD
2 blockedBeds | 1 | [{'id': 'bedName', 'value': '52_2'}] | EFGH
3 unOccupied | 1 | [{'id': 'bedName', 'value': '53_1'}] | EFGH
我需要以下格式:
blockedBeds | unOccupied | unitCode
0 | '23_1' | NaN | ABCD
1 | NaN | '20a_2' | ABCD
2 | NaN | '22a_1' | ABCD
3 | '52_2' | NaN | EFGH
4 | NaN | '53_1' | EFGH
我似乎無法獲得嵌套床數據。 我非常感謝您的幫助。
您應該從循環中創建一個字典列表,並使用它來創建 dataframe。
vals = []
for item in parsed_json:
unit_code = item['unitCode']
for col in item['columns']:
for hd in col['hoverDetails']:
vals.append({'unitCode': unit_code,
col['id']: hd['value']})
pd.DataFrame(vals)
Output
unitCode blockedBeds unOccupied
0 ABCD 23_1 NaN
1 ABCD NaN 20a_2
2 ABCD NaN 22a_1
3 EFGH 52_2 NaN
4 EFGH NaN 53_1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.