gpt4 book ai didi

python - 使用 Pandas 将 JSON 转换为多个 DataFrame

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:03 24 4
gpt4 key购买 nike

我有以下 JSON 数据:

{
"categories": [
{
"category_id": "11decadd",
"name": "Com",
"category_type": "Type",
"position": 5,
"vela_defined": True,
"created_at": "2017-02-15 01:49:23 -0700",
"updated_at": "2017-02-15 01:49:23 -0700"
},
{
"category_id": "c7010763",
"name": "none",
"category_type": "EquipmentStatus",
"position": 1,
"vela_defined": True,
"created_at": "2017-02-15 01:49:23 -0700",
"updated_at": "2018-03-01 04:20:38 -0700"
}
],
"customizable_categories": [
{
"customizable_category_id": "435ae18b",
"name": "NA",
"category_id": "11decadd",
"position": 1,
"created_at": "2017-02-15 01:49:23 -0700",
"updated_at": "2017-02-15 01:49:23 -0700"
},
{
"customizable_category_id": "51e607d8",
"name": "Third Party",
"category_id": "fafab667",
"position": 2,
"created_at": "2017-02-15 01:49:23 -0700",
"updated_at": "2017-02-15 01:49:23 -0700"
}
],
"equipment_category_status_sets": [

]
}

我试图将它变成 3x Pandas 数据帧(由 JSON 顶级条目命名)

但似乎根本无法加载它。有什么建议吗?

最佳答案

我认为需要使用 DataFrame 构造函数来理解 DataFrame 字典:

dfs = {k:pd.DataFrame(v) for k, v in d.items()}

print (dfs['categories'])

category_id ... vela_defined
0 11decadd ... True
1 c7010763 ... True

[2 rows x 7 columns]

print (dfs['customizable_categories'])

category_id ... updated_at
0 11decadd ... 2017-02-15 01:49:23 -0700
1 fafab667 ... 2017-02-15 01:49:23 -0700

[2 rows x 6 columns]

print (dfs['equipment_category_status_sets'])

Empty DataFrame
Columns: []
Index: []

关于python - 使用 Pandas 将 JSON 转换为多个 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50391836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com