I have a dataseries (mean_ratings) like below:
A. Morin 39203.340909
AMMA 53476.250000
Acalli 43798.333333
Adi 41277.500000
Aequare (Gianduja) 45185.000000
...
Zokoko 44615.000000
Zotter 46138.326531
hello cocoa 26162.333333
hexx 35043.333333
twenty-four blackbirds 36877.666667
I sort this series with this code:
best_ratings = mean_ratings.sort_values(0,ascending=False)
Idilio (Felchlin) 70040.000000
Dole (Guittard) 65662.500000
Chocola'te 64725.000000
Christopher Morel (Felchlin) 63787.500000
Madecasse (Cinagra) 62454.166667
Salgado 61285.000000
Compania de Chocolate (Salgado) 61285.000000
Chloe Chocolat 61285.000000
Oialla by Bojessen (Malmo) 61285.000000
Rozsavolgyi 61285.000000
But I want to sort this, first based on values , and then for equal values based on alphabetic indexes. How can I do this simultaneously?
CodePudding user response:
You have to use kind='mergesort'
on the second sort. Something like:
df.sort_index().sort_values(0, ascending=False, kind='mergesort')
CodePudding user response:
Pass .sort_values()
a list of the column names you wish to sort by to sort at multiple levels!
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html
>>> df = pd.DataFrame({'foo':['a', 'a', 'b', 'c'], 'bar':[2,1,4,3]})
>>> df
foo bar
0 a 2
1 a 1
2 b 4
3 c 3
>>> df.sort_values(["foo", "bar"])
foo bar
1 a 1
0 a 2
2 b 4
3 c 3
>>> df.sort_values(["bar", "foo"])
foo bar
1 a 1
0 a 2
3 c 3
2 b 4