DF有
,manufacturer,project,type,Metric
0,Honda,project_a,sedan,10
1,Honda,project_a,suv,20
2,Honda,project_a,hatchback,2
3,Toyota,project_a,sedan,11
4,Toyota,project_a,suv,21
5,Toyota,project_a,hatchback,3
6,Honda,project_b,sedan,101
7,Honda,project_b,suv,201
8,Honda,project_b,hatchback,21
9,Toyota,project_b,sedan,111
10,Toyota,project_b,suv,211
11,Toyota,project_b,hatchback,31
DF想要
project_a project_b
type hatchback sedan suv hatchback sedan suv
manufacturer
Honda 2 10 20 . 21 101 201
Toyota 3 11 21 . 31 111 211
目前我有-
df1 ['Metric']。groupby([df1 ['manufacturer'],df1 ['type']])。mean()。unstack()
type hatchback sedan suv
manufacturer
Honda 11.5 55.5 110.5
Toyota 17.0 61.0 116.0
如何将project_a和project_b扩展为更高级别的列?
答案 0 :(得分:0)
好像您想pivot_table
多列
df.pivot_table(columns=['project', 'type'], index=['manufacturer'], values=['Metric'])
project project_a project_b
type hatchback sedan suv hatchback sedan suv
manufacturer
Honda 2 10 20 21 101 201
Toyota 3 11 21 31 111 211