Sort dataframe by first column, Pandas

15,534

I think you can select first column by tst.columns[0], better is use sort_values because sort return warning:

FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

sort = tst.sort_values(tst.columns[0], ascending = False)

print (tst)
                Mean
SIMULATION          
Sim_213     0.845990
Sim_758     1.351917
Sim_830     0.921284
Sim_295     0.870272
Sim_215     1.072942
Sim_830     0.921284
Sim_295     0.870272
Sim_440     0.822394

print (tst.columns[0])
Mean

sort = tst.sort_values(tst.columns[0], ascending = False)
print (sort)
                Mean
SIMULATION          
Sim_758     1.351917
Sim_215     1.072942
Sim_830     0.921284
Sim_830     0.921284
Sim_295     0.870272
Sim_295     0.870272
Sim_213     0.845990
Sim_440     0.822394
Share:
15,534
Carmen
Author by

Carmen

Updated on September 15, 2022

Comments

  • Carmen
    Carmen over 1 year

    I have a dataframe with one column, which I would like to sort. Typing following code gives me a sorted dataframe:

    sort = tst.sort(["Mean"], ascending = False)
    
                    Mean
    SIMULATION          
    Sim_758     1.351917
    Sim_215     1.072942
    Sim_830     0.921284
    Sim_295     0.870272
    Sim_213     0.845990
    Sim_440     0.822394
    

    This will be part of a function, which will be applied to other dataframes. For this reason I need to sort the dataframe without mentioning the column name "mean".

    Is there a way to sort a dataframe by the values of a column, only indicating the position of the column?