python - 使用 Pandas 将 JSON 转换为多个 DataFrame
问题描述
我有以下 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
我认为需要对s 的构造函数进行字典理解dictionary of 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: []
推荐阅读
- r - raster: resample() 函数中方法的修改
- azure - 无法将 Azure Sphere 设备用作 TCP 客户端
- python-3.x - 使用 .apply() 创建列 带有字符串的 Pandas
- java - 每次我使用“再次播放”按钮时,我的井字游戏应用程序都会崩溃
- eclipse - Eclipse 2018-12 版本 - 导入项目菜单中缺少 SVN 选项
- java - 是否可以在给定 ANTLR3(.g) 文件的情况下生成 java 访问者类
- css - React Bootstrap 选择选项中的标志呈现为 [object, object]
- xamarin - Xamarin.Forms Accessibilty VoiceReader
- php - 为尝试转换传递的无效字符,这些已被忽略 - 将 Laravel 项目推送到 heroku
- reactjs - 通用道具类型箭头