Sort dataframe by first column, Pandas

空扰寡人 提交于 2020-01-11 09:38:52

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/40462675/sort-dataframe-by-first-column-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!