Sample data:
mdf = pd.DataFrame([[1,2,50],[1,2,20],
[1,5,10],[2,8,80],
[2,5,65],[2,8,10]
], columns=['src','dst','n']); mdf
src dst n
0 1 2 50
1 1 2 20
2 1 5 10
3 2 8 80
4 2 5 65
5 2 8 10
groupby()
gives a two-level multi-index:
test = mdf.groupby(['src','dst'])['n'].agg(['sum','count']); test
sum count
src dst
1 2 70 2
5 10 1
2 5 65 1
8 90 2
Question: how to sort this DataFrame by src
ascending and then by sum
descending?
I'm a beginner with pandas, learned about sort_index() and sort_values(), but in this task it seems that I need both simultaneously.
Expected result, under each "src" sorting is determined by the "sum":
sum count
src dst
1 2 70 2
5 10 1
2 8 90 2
5 65 1