Python 我想从我的数据帧创建一个多嵌套的json
我有一个熊猫数据框,格式如下:-Python 我想从我的数据帧创建一个多嵌套的json,python,pandas,to-json,Python,Pandas,To Json,我有一个熊猫数据框,格式如下:- EMPCODE|Indicator|CustNAME 1 CASA Raja 1 CASA lala 1 CASA dada 1 TL Nan 1 l Nan 1 p Nan 1 q Nan
EMPCODE|Indicator|CustNAME
1 CASA Raja
1 CASA lala
1 CASA dada
1 TL Nan
1 l Nan
1 p Nan
1 q Nan
2 CASA Crick
2 CASA Flick
2 TL Nan
2 l Nan
2 p Nan
2 q Nan
我想把它转换成一个嵌套的json
我尝试了各种不同的方法,包括groupby()、apply(),但我无法获得所需json格式的输出。从下面提到的代码中,我得到了两名员工的重复custNAme值
group = merge_hr_anr.groupby('EMPCODE_y').groups
group1 = merge_hr_anr.groupby("EMPNAME").groups
for variable in range(a):
d = {'EMPCODE_y': list(group.keys())[variable],'EMPNAME':
list(group1.keys())[variable] ,'Indicators': [{'IndicatorName':
merge_hr_anr.loc[i, 'IndicatorName']} for i in list(group.values())
[variable].unique()]}
d['Indicators'] = list(map(dict,sorted(set(map(lambda x:
tuple(x.items()),d['Indicators'])), key=list(map(lambda x:
tuple(x.items()),d['Indicators'])).index)))
d['Performance'] = [{i['IndicatorName']:
(merge_hr_anr.loc[merge_hr_anr['IndicatorName'].eq(i['IndicatorName']),"CUSTNAME"]).dropna().tolist()} for i in d['Indicators']]
我希望输出是
{
"EMPCODE": "1",
"Indicators": [
{
"IndicatorName": "CASA"
},
{
"IndicatorName": "TL"
},
{
"IndicatorName": "l"
},
{
"IndicatorName": "p"
},
{
"IndicatorName": "q"
}
]
"Performance":[
{
"CASA":[{"Custname":"Raja"},{"Custname":"lala"},{"Custname":"dada"}]
},
{
"TL":[]
}
{
"l":[]
}
{
"p":[]
}
{
"q":[]
}
]
}
{
"EMPCODE": "2",
"Indicators": [
{
"IndicatorName": "CASA"
},
{
"IndicatorName": "TL"
},
{
"IndicatorName": "l"
},
{
"IndicatorName": "p"
},
{
"IndicatorName": "q"
}
]
"Performance":[
{
"CASA":[{"Custname":"Crick"},{"Custname":"Flick"}]
},
{
"TL":[]
}
{
"l":[]
}
{
"p":[]
}
{
"q":[]
}
]
}
尝试以下代码,构造一个
dict
ionary:
group = merge_hr_anr.groupby('EMPCODE').groups
d = {'EMPCODE': list(group.keys())[0], 'Indicators': [{'IndicatorName': merge_hr_anr.loc[i, 'Indicator']} for i in list(group.values())[0].unique()]}
d['Indicators'] = list(map(dict,sorted(set(map(lambda x: tuple(x.items()),d['Indicators'])), key=list(map(lambda x: tuple(x.items()),d['Indicators'])).index)))
d['Performance'] = [{i['IndicatorName']: merge_hr_anr.loc[merge_hr_anr['Indicator'].eq(i['IndicatorName']), 'CustNAME'].dropna().tolist()} for i in d['Indicators']]
print(d)
输出:
{'EMPCODE': 1, 'Indicators': [{'IndicatorName': 'CASA'}, {'IndicatorName': 'TL'}, {'IndicatorName': 'l'}, {'IndicatorName': 'p'}, {'IndicatorName': 'q'}], 'Performance': [{'CASA': ['Raja', 'lala', 'dada']}, {'TL': []}, {'l': []}, {'p': []}, {'q': []}]}
要编写.json
文件:
with open('outvalue7.json', 'w') as f:
f.write(str(d))
尝试以下代码,构造一个
dict
ionary:
group = merge_hr_anr.groupby('EMPCODE').groups
d = {'EMPCODE': list(group.keys())[0], 'Indicators': [{'IndicatorName': merge_hr_anr.loc[i, 'Indicator']} for i in list(group.values())[0].unique()]}
d['Indicators'] = list(map(dict,sorted(set(map(lambda x: tuple(x.items()),d['Indicators'])), key=list(map(lambda x: tuple(x.items()),d['Indicators'])).index)))
d['Performance'] = [{i['IndicatorName']: merge_hr_anr.loc[merge_hr_anr['Indicator'].eq(i['IndicatorName']), 'CustNAME'].dropna().tolist()} for i in d['Indicators']]
print(d)
输出:
{'EMPCODE': 1, 'Indicators': [{'IndicatorName': 'CASA'}, {'IndicatorName': 'TL'}, {'IndicatorName': 'l'}, {'IndicatorName': 'p'}, {'IndicatorName': 'q'}], 'Performance': [{'CASA': ['Raja', 'lala', 'dada']}, {'TL': []}, {'l': []}, {'p': []}, {'q': []}]}
要编写.json
文件:
with open('outvalue7.json', 'w') as f:
f.write(str(d))
您好,谢谢您的回复,但我有一个问题,我给这里的表只是我必须使用的表的子集。在最初的问题中,我有多个员工,他们有不同的客户。因此,当我迭代时,它会显示每个员工的相同客户详细信息。我编辑了我的问题。请您仔细阅读并帮助我进行头脑风暴,但找不到解决方案。嗨,谢谢您的回复,但我有一个问题,我在这里给出的表只是我必须使用的表的一个子集。在最初的问题中,我有多个员工,他们有不同的客户。因此,当我迭代时,每个员工都会显示相同的客户详细信息。我编辑了我的问题。请您仔细阅读并帮助我进行头脑风暴,但找不到解决方案