带有嵌套JSON的熊猫json_normalize

时间:2020-04-08 20:16:54

标签: json pandas normalize

我尝试了许多替代方法,结果更好:

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            
                }
            ]
        }
    ]
}

所需输出 enter image description here

1 个答案:

答案 0 :(得分: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"])

结果: enter image description here