Python 如何按数据帧的两列分组,并将其他列转换为以列标题为键的dict
数据帧:Python 如何按数据帧的两列分组,并将其他列转换为以列标题为键的dict,python,pandas,json-normalize,Python,Pandas,Json Normalize,数据帧: id id_2 salary title allowance name 0420 13.28 100000 director No Tom 0420 13.28 70000 developer Yes Sam 0110 13.12 120000 director No Dave 0110 13.12 75000 developer Yes shaun
id id_2 salary title allowance name
0420 13.28 100000 director No Tom
0420 13.28 70000 developer Yes Sam
0110 13.12 120000 director No Dave
0110 13.12 75000 developer Yes shaun
[{
"id": 420,
"id_2": 13.28,
"attributes":[
{ "salary": 100000,
"title":"director",
"allowance":"No",
"name": "Tom"
},
{ "salary": 70000,
"title": "developer",
"allowance":"Yes",
"name": "Sam"
}
]
},
{
"id": 110,
"id_2": 13.12,
"attributes":[
{ "salary": 120000,
"title":"director",
"allowance":"No",
"name": "Dave"
},
{ "salary": 75000,
"title": "developer",
"allowance":"Yes",
"name": "shaun"
}
]
}
]
Groupby id和id_2并将其余列转换为带有列标题的dict
我为此写了一个循环,我认为这不是python的方式,请让我知道如何使用熊猫
所需输出:
id id_2 salary title allowance name
0420 13.28 100000 director No Tom
0420 13.28 70000 developer Yes Sam
0110 13.12 120000 director No Dave
0110 13.12 75000 developer Yes shaun
[{
"id": 420,
"id_2": 13.28,
"attributes":[
{ "salary": 100000,
"title":"director",
"allowance":"No",
"name": "Tom"
},
{ "salary": 70000,
"title": "developer",
"allowance":"Yes",
"name": "Sam"
}
]
},
{
"id": 110,
"id_2": 13.12,
"attributes":[
{ "salary": 120000,
"title":"director",
"allowance":"No",
"name": "Dave"
},
{ "salary": 75000,
"title": "developer",
"allowance":"Yes",
"name": "shaun"
}
]
}
]
- 没有一个单行熊猫参数可以在您请求的形状中提供
的dicts
列表
- 用于选择组
是表示用于分组的值的g
元组
是groupby值的数据帧,d
g
- 用于迭代每个组的行
- 返回由第一个
表示的
,因为它不是必需的索引
- 返回
,从中删除数据
中的标签,然后使用将剩余部分转换为分组列表
,并将其附加到目录
,列表
附件列表
- 循环遍历组的所有行后,将
作为值分配给att_list
group['attributes']
- 返回由第一个
- 迭代每个组后,将
,dict
附加到group
dict\u列表中
可以通过以下方式转换回数据帧:dict\u list
df=pd.json\u规范化(dict\u list,'attributes',meta=groupby\u list)
dict_list=list()
groupby_list=['id','id_2']
对于df.groupby(groupby_列表)中的g、d:
group=dict(zip(groupby_列表,g))
附件列表=列表()
对于x,d.iterrows()中的数据:
data=data.drop(标签=groupby_列表)
att_list.append(data.to_dict())
组['attributes']=附件列表
dict_list.append(组)
命令列表
:
[{'attributes':[{'allowment':'No',
“姓名”:“戴夫”,
“工资”:120000,
“职务”:“董事”},
{‘津贴’:‘是’,
“姓名”:“肖恩”,
“工资”:75000,
'title':'developer'}],
id:110,,
'id_2':13.12},
{'attributes':[{'allowment':'No',
“姓名”:“汤姆”,
“工资”:100000美元,
“职务”:“董事”},
{‘津贴’:‘是’,
'姓名':'山姆',
“工资”:70000,
'title':'developer'}],
“id”:420,
'id_2':13.28}]