按第一列Pandas对数据框进行排序 [英] Sort dataframe by first column, Pandas

查看:197
本文介绍了按第一列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

这将是功能的一部分,该功能将应用于其他数据框.因此,我需要对数据框进行排序,而不必提及列名"mean".

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?

推荐答案

我认为您可以通过tst.columns[0]选择第一列,最好使用

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

FutureWarning:不建议使用sort(columns = ....),请使用sort_values(by = .....)

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

这篇关于按第一列Pandas对数据框进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆