Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rank by grouby column aggregate

Tags:

python

pandas

I want to create a column manager_rank that ranks a manager by the sum of returns. I have come up with one solution posted below but was hoping if someone else had something more elegant.

import pandas as pd
df = pd.DataFrame([['2012', 'A', 1], ['2012', 'B', 4], ['2011', 'A', 5], ['2011', 'B', 4]],
                 columns=['year', 'manager', 'return'])

Desired result:

   year manager  return  manager_rank
0  2012       A       1             2
1  2011       A       5             2
2  2012       B       4             1
3  2011       B       4             1
like image 902
Kamil Sindi Avatar asked Jan 06 '23 22:01

Kamil Sindi


1 Answers

df['ranking'] = df.groupby('manager')['return'].transform(np.sum).rank(ascending=False, method='dense')

   year manager  return  ranking
0  2012       A       1        2
1  2012       B       4        1
2  2011       A       5        2
3  2011       B       4        1
like image 138
Stefan Avatar answered Jan 16 '23 03:01

Stefan