1df.groupby(['job']).apply(lambda x: (x.groupby('source')
2 .sum()
3 .sort_values('count', ascending=False))
4 .head(3))
1import pandas as pd
2
3df = pd.DataFrame({'A':[1, 2, 3, 4, 5, 6],
4 'B':[0, 1, 2, 0, 1, 2]})
5
6df.groupby('B').apply(lambda x: x.sort_values('A', ascending = True))