Python Pandas:Pivot groupby对象

时间:2014-05-09 13:45:35

标签: python pandas

我的DataFrame看起来像这样;

Name,Comp,Dept,Salary,Allowance
Sam,Google,Sales,10000,4500
Sandra,Google,Sales,2300,3450
Allan,Google,Sales,2400,1000
Helen,Google,Mktg,3456,700
Rick,Google,Mktg,5412,352
Farrow,Apple,Sales,9786,190
Isaac,Apple,Sales,4500,230
Tim,Apple,Mktg,4500,500
Ben,Apple,Mktg,3490,450
Julie,Apple,Mktg,4590,750

我想以下列格式获得2个输出;

输出1: - 这是两家公司的工资薪酬的平均值

Dept    Google  Apple
Sales   4900    7143
Mktg    4434    4193

输出2 - 这是Apple部门最高支付人员的价值。 &安培; '谷歌'

Dept    Google  Apple
Sales   10000   9786
Mktg    5412    4590

我到目前为止已经达到了;

data.groupby(['Dept','Comp'])['Salary'].mean().order(ascending=False)

Dept   Comp  
Sales  Apple     7143.000000
       Google    4900.000000
Mktg   Google    4434.000000
       Apple     4193.333333
Name: Salary, dtype: float64

如何调整结果以获得上述格式?

1 个答案:

答案 0 :(得分:1)

使用unstack

In [111]: means = df.groupby(['Comp', 'Dept'])['Salary'].mean()

In [112]: means.unstack(0).sort(ascending=False)
Out[112]: 
Comp         Apple  Google
Dept                      
Sales  7143.000000    4900
Mktg   4193.333333    4434

[2 rows x 2 columns]

In [110]: df.groupby(['Comp', 'Dept'])['Salary'].max().unstack(0).sort(ascending=False)
Out[110]: 
Comp   Apple  Google
Dept                
Sales   9786   10000
Mktg    4590    5412

[2 rows x 2 columns]