Amen_90
Amen_90

Reputation: 350

sort dataframe by two variables in pandas groupby

I have a DF that looks like this;

   Group     Name     Rank     Group_Sales_Total
0  A         Dave     4        10
1  A         Tom      2        10
2  A         Sara     1        10
3  A         Raul     3        10
4  B         Chris    3        20
5  B         John     4        20
6  B         Aarti    1        20
7  B         Amen     2        20

What I would like to do is, sort each member of each group by their respective rank and then sort the groups by their respective sales total. The result i'm aiming for should look like this.

   Group     Name     Rank     Group_Sales_Total
0  B         Aarti    1        20
1  B         Amen     2        20
2  B         Chris    3        20
3  B         John     4        20
4  A         Sara     1        10
5  A         Tom      2        10
6  A         Raul     3        10
7  A         Dave     4        10

I am able to sort the ranking out correctly for each group using;

DF.groupby('Group').apply(pd.DataFrame.sort_values, 'Rank', ascending=True).reset_index(drop=True)

However, when I also try to sort it by the group sales total, the sort either doesn't occur or messes up the order of the ranking. I've tried the following;

DF.groupby('Group').apply(pd.DataFrame.sort_values, ['Rank','Group_Sales_Total'], ascending=[True, False]).reset_index(drop=True)

Any help/insight on getting to the output would be greatly appreciated.

Kind regards

Upvotes: 2

Views: 574

Answers (1)

wwnde
wwnde

Reputation: 26676

 df.sort_values(['Group','Rank','Group_Sales_Total'], ascending=[False, True, False])



 Group   Name  Rank  Group_Sales_Total
6     B  Aarti     1                 20
7     B   Amen     2                 20
4     B  Chris     3                 20
5     B   John     4                 20
2     A   Sara     1                 10
1     A    Tom     2                 10
3     A   Raul     3                 10
0     A   Dave     4                 10

Upvotes: 2

Related Questions