简体   繁体   English

如何按值对df行排序?

[英]How can I rank df rows by value?

df looks like below: Age, Sex... they are all index,with only one column named Importance df如下所示: Age, Sex...它们都是索引,只有一列名为“ Importance

                        Importance
Onset Delta               0.121048
Site of Onset - Limb      0.000036
Site of Onset - Bulbar    0.000382
Age                       0.008650
Sex                       0.000978
Race - Caucasian          0.001274
Race - Other              0.001776
Sodium_Dmax               0.007689

I would like to re-shape the df, by ranking rows according to Importance ,how could I do that? 我想通过根据Importance行进行排序来重塑df,我该怎么做? I tried 我试过了

groupby(['Importance'],as_index=False)

But not work Thanks 但是不行谢谢

Use the sort_values function: 使用sort_values函数:

test = df.sort_values('Importance')

assuming df is the dataframe 假设df是数据帧

If your data's structure is dataframe, you can use sort function: 如果数据的结构是数据框,则可以使用sort函数:

df.sort(['Importance'],ascending=True) or 
df.sort(['Importance'],ascending=False)

The "True" or "False" depends on your option, it means your data are listed in descending order or ascending order. “ True”或“ False”取决于您的选择,这意味着您的数据以降序或升序列出。

df.groupby() would be used if you wanted aggregations on the data, what you're looking for is df.sort_values() . 如果您想对数据进行聚合,则将使用df.groupby() ,而您正在寻找的是df.sort_values()

With df.sort_values() , you pass in the by string telling pandas which column to sort on. 使用df.sort_values() ,您将传入by字符串,告诉pandas对其进行排序。

For your code, I would expect df.sort_values(by='Importance') 对于您的代码,我希望使用df.sort_values(by='Importance')

You can assign the result of this to a new data frame, or pass in the inplace=true parameter to sort the df. 您可以将其结果分配给新的数据帧,或传递inplace=true参数对df进行排序。

You can view the documentation for the df.sort_values() method here 您可以在此处查看df.sort_values()方法的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM