ale19 ale19 - 2 months ago 18
Python Question

pandas: sort alphabetically if rows have same rank

Say I have a dataframe with two rows that have the same value:

testdf = pd.DataFrame({'a': ['alpha','beta','theta','delta','epsilon'],'b':[1,2,3,3,4]})

a b
0 alpha 1
1 beta 2
2 theta 3
3 delta 3
4 epsilon 4


Now I want to sort them based on column B:

testdf['rank'] = testdf['b'].rank(ascending=False)

a b rank
0 alpha 1 5.0
1 beta 2 4.0
2 theta 3 2.5
3 delta 3 2.5
4 epsilon 4 1.0


Since rows 2 and 3 have the same rank, I'd like to arrange them in alphabetical order for the viewer:

a b rank
0 alpha 1 5.0
1 beta 2 4.0
3 delta 3 2.5
2 theta 3 2.5
4 epsilon 4 1.0


But I'm not sure how to do this. Maybe I could group them by rank and order them...? I'm not sure how to do that in-place, though.

Answer

It's possible to sort by more than one column using sort_values, what you want is to sort by rank first, then by the a column, and if you want it to happen inplace, we use inplace=True like below:

In [14]: testdf.sort_values(['rank', 'a'], ascending=[False, True], inplace=True)

In [15]: testdf
Out[15]: 
         a  b  rank
0    alpha  1   5.0
1     beta  2   4.0
3    delta  3   2.5
2    theta  3   2.5
4  epsilon  4   1.0

Which is the result you want and inplace, if you don't use inplace=True the resulting dataframe will be returned from the operation.