将具有多个嵌套字典的 JSON 列表转换为 csv 或 excel

问题描述 投票:0回答:1

我有一个从网站下载的 JSON,该网站的主列表中有多个嵌套字典。这是它的一个非常简化的版本。

[
    {
        "id": 1,
        "attributes": {
            "autoNumber": 1,
            "make": "Ford",
            "model": "F150",
            "trim": "Lariat"
            },
            "engine": {
                "data": [
                    {
                        "id": 1,
                        "attributes": {
                            "engine": "5.0l v8 ",
                            "horsePower": "400",
                            "torque": "410"
                        }
                    },
                    {
                        "id": 2,
                        "attributes": {
                            "engine": "2.7l v6 ",
                            "horsePower": "325",
                            "torque": "300"
                        }
                    }
                ]
            }
    }
]

这是我用来转换为xlsx的代码

import json
import pandas as pd

# Load JSON data
with open('data.json') as json_file:
    data = json.load(json_file)

# Normalize JSON data to tabular format
df = pd.json_normalize(data)

df.to_excel('data.xlsx', index=False)

我希望所有不同的属性都在自己的列中。引擎数据可能是零到两个条目,并且应该每个条目一个。我遇到的问题是它将所有引擎数据放入一列中。

列f = [{'id': 1, '属性': {'引擎': '5.0l v8', '马力': '400', '扭矩': '410'}}, {'id': 2、'属性':{'发动机':'2.7l v6','马力':'325','扭矩':'300'}}]

我尝试了数据框爆炸,但它创建了额外的行而不是列。

python-3.x pandas dataframe
1个回答
0
投票

我建议从数据创建两个数据框,然后根据

id
将其合并在一起,例如:

import pandas as pd

data = [
    {
        "id": 1,
        "attributes": {
            "autoNumber": 1,
            "make": "Ford",
            "model": "F150",
            "trim": "Lariat",
        },
        "engine": {
            "data": [
                {
                    "id": 1,
                    "attributes": {
                        "engine": "5.0l v8 ",
                        "horsePower": "400",
                        "torque": "410",
                    },
                },
                {
                    "id": 2,
                    "attributes": {
                        "engine": "2.7l v6 ",
                        "horsePower": "325",
                        "torque": "300",
                    },
                },
            ]
        },
    }
]

df1 = []
for d in data:
    df1.append({"id": d["id"], **d["attributes"]})

df2 = []
for d in data:
    for engine_data in d["engine"]["data"]:
        df2.append(
            {
                "id": d["id"],
                "attribute_id": engine_data["id"],
                **engine_data["attributes"],
            }
        )

df1 = pd.DataFrame(df1)
df2 = pd.DataFrame(df2)

df_out = df1.merge(df2, on="id", how="outer")
print(df_out)

打印:

   id  autoNumber  make model    trim  attribute_id    engine horsePower torque
0   1           1  Ford  F150  Lariat             1  5.0l v8         400    410
1   1           1  Ford  F150  Lariat             2  2.7l v6         325    300
© www.soinside.com 2019 - 2024. All rights reserved.