Issue
My dataframe-
dfmodtestes
Account Key Name MonthSinceInception False
1 a0 Gu 20 0.5
1 a6 Gu 15 0.4
1 a9 Gu 35 0.9
2 89 Pa 70 0.8
2 01 Ra 08 0.1
My objective is to keep the account order the same, but based on monthsinceinception all other column orders must be sorted by decending goal -
dfmodtestes
Account Key Name MonthSinceInception False
1 a9 Gu 35 0.9
1 a6 Gu 20 0.5
1 a0 Gu 15 0.4
2 89 Pa 70 0.8
2 01 Ra 08 0.1
So as you can observe, Account order is the same but all other variables should change based on MonthsinceInception sort by descending
I attempted
dfmodwhtestes = dfmodwhtestes.sort_values(by = 'MonthsSinceInception', ascending = False)
But this just sorted the whole dataframe by descending based on month sinceinception, so what happend is account 2(month since inception =70) was in the first row then account 1 was in second row because month since inception is 35.
Solution
Try using sort_values
, note per docs ascending can accept a liset of bool:
df.sort_values(['Account', 'MonthSinceInception'], ascending=[True, False])
Output:
Account Key Name MonthSinceInception False
2 1 a9 Gu 35 0.9
0 1 a0 Gu 20 0.5
1 1 a6 Gu 15 0.4
3 2 89 Pa 70 0.8
4 2 01 Ra 8 0.1
Answered By - Scott Boston
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.