1df.groupby(['job']).apply(lambda x: (x.groupby('source')
2 .sum()
3 .sort_values('count', ascending=False))
4 .head(3))
1In[34]: df.sort_values(['job','count'],ascending=False).groupby('job').head(3)
2
3Out[35]:
4 count job source
54 7 sales E
62 6 sales C
71 4 sales B
85 5 market A
98 4 market D
106 3 market B
11