【发布时间】:2021-09-07 18:12:21
【问题描述】:
您好,我正在尝试检索此数据,其中有 2 个嵌套字典:
{
"metadata": {
"stations": [
{
"id": "S108",
"device_id": "S108",
"name": "Kuala Lumpur",
"location": {
"latitude": 3.1390,
"longitude": 101.6869
}
},
{
"id": "S118",
"device_id": "S118",
"name": "Bukit Bintang",
"location": {
"latitude":3.1468,
"longitude": 101.7113
}
}
],
"reading_type": "DBT 1M F",
"reading_unit": "deg C"
},
"items": [
{
"timestamp": "2021-06-20T15:05:00+08:00",
"readings": [
{
"station_id": "S108",
"value": 32.6
},
{
"station_id": "S118",
"value": 30.3
}
]
}
]
我想得到这样的结果: Result
我尝试了几种方法:
data = airtemp.json()
df = pd.json_normalize(data,record_path=['metadata', 'stations'])
df
data = airtemp.json()
df1 = pd.json_normalize(data,record_path=['items','readings'])
df1
有没有一种方法可以使用 json_norminalize 形成一个包含 station_id、名称、纬度、经度、时间戳和值的表,而不会分成 2 个表?
非常感谢!
【问题讨论】:
标签: json python-3.x pandas python-requests