1 回答
TA贡献1815条经验 获得超6个赞
df = (df.melt('Segment', var_name='a')
.groupby(['Segment','a'])['value']
.agg(['min','max','mean'])
.reset_index())
print (df)
Segment a min max mean
0 1 Age 35 54 43.000000
1 1 Data_Usage 10 20 14.000000
2 1 Revenue 300 450 383.333333
3 1 Voice_Usage 300 700 483.333333
4 2 Age 20 20 20.000000
5 2 Data_Usage 30 30 30.000000
6 2 Revenue 100 100 100.000000
7 2 Voice_Usage 150 150 150.000000
8 3 Age 17 32 24.500000
9 3 Data_Usage 15 18 16.500000
10 3 Revenue 200 800 500.000000
11 3 Voice_Usage 100 900 500.000000
如果想要多个统计信息,请使用DataFrameGroupBy.describe
:
df = (df.melt('Segment', var_name='a')
.groupby(['Segment','a'])['value']
.describe()
.reset_index())
print (df)
Segment a count mean std min 25% 50% \
0 1 Age 3.0 43.000000 9.848858 35.0 37.50 40.0
1 1 Data_Usage 3.0 14.000000 5.291503 10.0 11.00 12.0
2 1 Revenue 3.0 383.333333 76.376262 300.0 350.00 400.0
3 1 Voice_Usage 3.0 483.333333 202.072594 300.0 375.00 450.0
4 2 Age 1.0 20.000000 NaN 20.0 20.00 20.0
5 2 Data_Usage 1.0 30.000000 NaN 30.0 30.00 30.0
6 2 Revenue 1.0 100.000000 NaN 100.0 100.00 100.0
7 2 Voice_Usage 1.0 150.000000 NaN 150.0 150.00 150.0
8 3 Age 2.0 24.500000 10.606602 17.0 20.75 24.5
9 3 Data_Usage 2.0 16.500000 2.121320 15.0 15.75 16.5
10 3 Revenue 2.0 500.000000 424.264069 200.0 350.00 500.0
11 3 Voice_Usage 2.0 500.000000 565.685425 100.0 300.00 500.0
75% max
0 47.00 54.0
1 16.00 20.0
2 425.00 450.0
3 575.00 700.0
4 20.00 20.0
5 30.00 30.0
6 100.00 100.0
7 150.00 150.0
8 28.25 32.0
9 17.25 18.0
10 650.00 800.0
11 700.00 900.0
添加回答
举报