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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With