Multiple relational tables to nested JSON format using Python(使用 Python 将多个关系表转换为嵌套的 JSON 格式)
问题描述
我正在尝试通过使用 python/pandas 组合多个关系表来创建嵌套的 JSON 对象.我是 Python/pandas 的初学者,所以在这里寻求帮助...
在下面的例子中,为了简单起见,我使用 CSV 文件而不是表格
<块引用>Table1.csv
Emp_id、性别、年龄
1, 男, 32
2, 男, 35
3、F、31
Table2.csv
Emp_id、月份、奖励
3000 年 8 月 1 日
1, 九月, 3500
2000 年 10 月 1 日
1500 年 8 月 2 日
5000 年 8 月 3 日
2400 年 9 月 3 日
我想创建一个像下面这样的 JSON 对象
<块引用>*所需输出:
<代码>{数据": [{员工":1,性别":M,年龄":32,激励": [{八月":3000,九月":3500,十月":2000}],雇员":2,性别":M,年龄":35,激励": [{八月":1500}],员工":3,性别":F,年龄":31,激励": [{八月":5000,九月":2400}]}]}
使用 merge
先用左连接,然后 groupby
带有用于字典的 lambda 函数并转换 to_dict
,最后添加顶部 key
值并转换为 json
:
d = (df1.merge(df2, on='Emp_id', how='left').groupby(['Emp_id','Gender','Age'])['Month','Incentive'].apply(lambda x: [dict(x.values)]).reset_index(name='Incentive').to_dict(orient='记录'))#打印(d)导入jsonjson = json.dumps({'数据':d})
<小时>
print (json){数据": [{Emp_id":1,"性别": "M",年龄":32,激励": [{八月":3000,九月":3500,十月":2000}]}, {Emp_id":2,"性别": "M",年龄":35,激励": [{八月":1500}]}, {Emp_id":3,"性别": "F",年龄":31,激励": [{八月":5000,九月":2400}]}]}
I'm trying to create nested JSON object by combining more than one relational tables using python/pandas. I'm a beginner in Python/pandas, so looking for bit of a help here...
In the following example, instead of tables, I'm using CSV files just to keep it simple
Table1.csv
Emp_id, Gender, Age
1, M, 32
2, M, 35
3, F, 31Table2.csv
Emp_id, Month, Incentive
1, Aug, 3000
1, Sep, 3500
1, Oct, 2000
2, Aug, 1500
3, Aug, 5000
3, Sep, 2400
I want to create a JSON object like below
*Required output:
{
"data": [{
"employee": 1,
"gender": M,
"age": 32,
"incentive": [{
"aug": 3000,
"sep": 3500,
"oct": 2000
}],
"employee": 2,
"gender": M,
"age": 35,
"incentive": [{
"aug": 1500
}],
"employee": 3,
"gender": F,
"age": 31,
"incentive": [{
"aug": 5000,
"sep": 2400
}]
}]
}
Use merge
with left join first, then groupby
with lambda function for dictionaries and convert to_dict
, last add top key
value and convert to json
:
d = (df1.merge(df2, on='Emp_id', how='left')
.groupby(['Emp_id','Gender','Age'])['Month','Incentive']
.apply(lambda x: [dict(x.values)])
.reset_index(name='Incentive')
.to_dict(orient='records')
)
#print (d)
import json
json = json.dumps({'data':d})
print (json)
{
"data": [{
"Emp_id": 1,
"Gender": "M",
"Age": 32,
"Incentive": [{
"Aug": 3000,
"Sep": 3500,
"Oct": 2000
}]
}, {
"Emp_id": 2,
"Gender": "M",
"Age": 35,
"Incentive": [{
"Aug": 1500
}]
}, {
"Emp_id": 3,
"Gender": "F",
"Age": 31,
"Incentive": [{
"Aug": 5000,
"Sep": 2400
}]
}]
}
这篇关于使用 Python 将多个关系表转换为嵌套的 JSON 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Python 将多个关系表转换为嵌套的 JSON 格式
基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01