多重关系表嵌套JSON格式使用Python

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

我试图通过结合使用python /熊猫多个关系表来创建嵌套JSON对象。我在Python /大熊猫初学者,所以找的帮助这里有点...

在下面的例子,而不是表,我使用的CSV文件,只是为了保持简单

Table1.csv

EMP_ID,性别,年龄 1,M,32 2,M,35 3,F,31

Table2.csv

EMP_ID,月,奖励 1,八月,3000 1,九月,3500 1,2000年10月 2,八月,1500 3,八月,5000 3,九月,2400

我想创建一个像下面一个JSON对象

*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
        }]
    }]
}
python json pandas nested
1个回答
1
投票

使用merge与LEFT JOIN第一,然后用lambda函数groupby的字典和转换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='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
        }]
    }]
}
© www.soinside.com 2019 - 2024. All rights reserved.