Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort dataframe by first column, Pandas

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?

like image 291
Carmen Avatar asked Nov 07 '16 10:11

Carmen


1 Answers

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
like image 57
jezrael Avatar answered Sep 22 '22 07:09

jezrael