假设我有以下两个Json。
a={"id": "TUxNQkFHVUNBTTA0",
"name": "Campestre 1a. Secc.",
"city": {
"id": "TUxNQ0FHVTk2NjY",
"name": "Aguascalientes"
},
"state": {
"id": "TUxNUEFHVTMwNjE",
"name": "Aguascalientes"
},
"country": {
"id": "MX",
"name": "Mexico"
},
"geo_information": None,
"subneighborhoods": [
]
}
b={
"id": "TUxNTUxNQkFHVTNOSg",
"name": "Aeropuerto Aguascalientes (Lic. Jesus Teran Peredo)",
"city": {
"id": "TUxNQ0FHVTk2NjY",
"name": "Aguascalientes"
},
"state": {
"id": "TUxNUEFHVTMwNjE",
"name": "Aguascalientes"
},
"country": {
"id": "MX",
"name": "Mexico"
},
"geo_information": {
"location": {
"latitude": 21.701155,
"longitude": -102.31439
}
},
"subneighborhoods": [
]
}
print b
我想创建一个包含下以下列的表"位置":
位置 = 熊猫。DataFrame(columns=['city_id
', 'city_name', 'name', 'latitud', 'longitud', 'country_id', 'country_name', 'state_id', 'state_name', 'subneighborhoods', 'id'](预期具有以下数据:
我希望有下表
TUxNQkFHVUNBTTA0, Campestre 1a. Secc., TUxNQ0FHVTk2NjY, Aguascalientes, TUxNUEFHVTMwNjE, Aguascalientes, MX, Mexico, Null, Null, []
TUxNTUxNQkFHVTNOSg, Aeropuerto Aguascalientes (Lic. Jesus Teran Peredo), TUxNQ0FHVTk2NjY, Aguascalientes, TUxNUEFHVTMwNjE, Aguascalientes, MX, Mexico, 21.701155, -102.31439, []
由于在"a"中geo_information是 None,我无法创建表。我如何解决这个问题?
谢谢!
你试过json_normalizer
吗?它将按照您的要求执行,只是使用点而不是下划线。
In[1]: from pandas.io.json import json_normalize
In[2]: pd.DataFrame(json_normalize([a,b]))
Out[2]:
city.id city.name country.id country.name geo_information
0 TUxNQ0FHVTk2NjY Aguascalientes MX Mexico NaN
1 TUxNQ0FHVTk2NjY Aguascalientes MX Mexico NaN
geo_information.location.latitude geo_information.location.longitude
0 NaN NaN
1 21.701155 -102.31439
id name
0 TUxNQkFHVUNBTTA0 Campestre 1a. Secc.
1 TUxNTUxNQkFHVTNOSg Aeropuerto Aguascalientes (Lic. Jesus Teran Pe...
state.id state.name subneighborhoods
0 TUxNUEFHVTMwNjE Aguascalientes []
1 TUxNUEFHVTMwNjE Aguascalientes []
(但是,它会保持subneighborhoods
完好无损,这不一定是您想要的(