I have a pandas dataframe which looks like this:
X Y Z
AB bat 0.51184607
AB ball 0.45687276
AB cat 0.5428388
CD apple 0.89665717
CD tiger 0.6785901
CD bench 0.787102
CD dragon 0.8066087
I want to sort the dataframe based on column Z values in descending and accordingly get the respective Y column values but keeping column X as constant. So the output should look like:
X Y Z
AB cat 0.5428388
AB bat 0.51184607
AB ball 0.45687276
CD apple 0.89665717
CD dragon 0.8066087
CD bench 0.787102
CD tiger 0.6785901
How can I effectively do this in pandas
df.sort_values(['X','Z'], ascending=[True,False])?Xmust remain fixed but does not have an alphanumeric order, it would have to first be converted to dtypecategoryas shown in this answer.