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 格式
				
        
 
            
        - ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
 - 我如何卸载 PyTorch? 2022-01-01
 - 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
 - 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
 - 我如何透明地重定向一个Python导入? 2022-01-01
 - 使用 Cython 将 Python 链接到共享库 2022-01-01
 - YouTube API v3 返回截断的观看记录 2022-01-01
 - 如何使用PYSPARK从Spark获得批次行 2022-01-01
 - CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
 - 计算测试数量的Python单元测试 2022-01-01
 
