数据框:
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
Groupby id 和 id_2 并将其余列转换为带有列标题的dict
我为此写了一个循环,我认为这不是 python 方式,请让我知道如何使用 pandas 来实现。
所需输出:
[{
"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"
}
]
}
]