python-3.xpandassortingalphabetical-sort

Failing to sort pandas dataframe by values in descending order and then alphabetically in ascending order


A slice of my dataframe, df, is like this, so you can reproduce it.

data ={'feature_name': ['nite', 'thank', 'ok', 'havent', 'done', 'beverage', 'yup', 'lei','thanx', 'okie', '146tf150p', 'home', 'too', 'anytime',
       'where', '645', 'er', 'tick', 'blank'], 'values':[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.98, 0.98] }
df = pd.DataFrame(data)
df.set_index('feature_name',inplace=True)
dfs=df.sort_index(ascending=True).sort_values(by = ['values'], ascending=False)
dfs

my output is this.

            values
feature_name    
146tf150p   1.00
645         1.00
where       1.00
too         1.00
thanx       1.00
thank       1.00
okie        1.00
ok          1.00
nite        1.00
lei         1.00
home        1.00
havent      1.00
er          1.00
done        1.00
beverage    1.00
anytime     1.00
yup         1.00
blank       0.98
tick        0.98

I do not quite understand why it is not like this? It really should work yet it does not work as expected.


146tf150p   1.00
645         1.00
anytime     1.00
beverage    1.00
done        1.00
er          1.00
haven't     1.00
home        1.00
...

How can I fix this?


Solution

  • Get rid of the set_index and use sort_values on both values and feature_name:

    print (df.sort_values(by = ['values',"feature_name"], ascending=(False, True)))
    
       feature_name  values
    10    146tf150p    1.00
    15          645    1.00
    13      anytime    1.00
    5      beverage    1.00
    4          done    1.00
    16           er    1.00
    3        havent    1.00
    11         home    1.00
    7           lei    1.00
    0          nite    1.00
    2            ok    1.00
    9          okie    1.00
    1         thank    1.00
    8         thanx    1.00
    12          too    1.00
    14        where    1.00
    6           yup    1.00
    18        blank    0.98
    17         tick    0.98