我尝试了许多替代方法,并且更好的结果是使用此语句:
json_normalize(a['solution'][0]['tour'])
我一次只能看到一个游览团。 vehicle_id 0信息,我需要它们全部在一起。谢谢。
JSON是:
{
"total_clusters": 6,
"solution": [
{
"vehicles_id": "0",
"vehicles_location": {
"lat": "",
"lng": ""
},
"tour": [
{
"shipping_id": "4a4b0750-63a7-11ea-8955-43fcb2cd860a",
"type": "dropoff",
"location_id": "797",
"coordinates": {
"lat": "-34.545736",
"lng": "-58.488340"
},
"cluster": 0
},
{
"shipping_id": "75e5a2c0-6314-11ea-b657-ddd473c629a3",
"type": "dropoff",
"location_id": "114",
"coordinates": {
"lat": "-34.568707",
"lng": "-58.452963"
},
"cluster": 0
}
]
},
{
"vehicles_id": "1",
"vehicles_location": {
"lat": "",
"lng": ""
},
"tour": [
{
"shipping_id": "c83ac7c0-51c4-11ea-9aef-973de7785221",
"type": "pickup",
"location_id": "687",
"coordinates": {
"lat": "-34.592824",
"lng": "-58.375457"
},
"cluster": 1
},
{
"shipping_id": "b5a295c0-51c4-11ea-b36d-651ee769ca89",
"type": "pickup",
"location_id": "687",
"coordinates": {
"lat": "-34.592824",
"lng": "-58.375457"
},
"cluster": 1
}
]
}
]
}
您需要将record_path
参数传递给json_normalize
。
从docs:
record_path:str或str列表,默认为无
每个对象到记录列表的路径。如果未通过,则数据将被认为是一个记录数组。
import pandas as pd
import json
raw_json_data = """{contents_of_your_json_here}"""
json_data = json.loads(raw_json_data)
df = pd.json_normalize(json_data, ["solution", "tour"])