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   31   GroupB  Name2   22   GroupB  Name1   13   GroupA  Name6   64   GroupA  Name5   55   GroupA  Name4   4

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