Skip to content Skip to sidebar Skip to footer

Sorting Within A Pandas Group Without Changing Group Position

I am trying to sort within a pandas group without actually changing the group position in the Dataframe. The original dataframe is in this format: group name revenue 0 Grou

Solution 1:

Something like:

df.sort(['group', 'revenue'], ascending=False).reset_index(drop=True)

It gives me

    group   name    revenue
0   GroupB  Name3   3
1   GroupB  Name2   2
2   GroupB  Name1   1
3   GroupA  Name6   6
4   GroupA  Name5   5
5   GroupA  Name4   4

Post a Comment for "Sorting Within A Pandas Group Without Changing Group Position"