如何按值对 df 行进行排名?
How can I rank df rows by value?
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
我想重新塑造 df,根据 Importance
对行进行排名,我该怎么做?
我试过了
groupby(['Importance'],as_index=False)
但是不行
谢谢
使用sort_values函数:
test = df.sort_values('Importance')
假设 df
是数据帧
df.groupby()
如果您想对数据进行聚合,则可以使用,您要查找的是 df.sort_values()
.
使用 df.sort_values()
,您传入 by
字符串告诉 pandas 要排序的列。
对于您的代码,我希望 df.sort_values(by='Importance')
你可以把这个结果赋给一个新的dataframe,或者传入inplace=true
参数对df进行排序。
您可以查看 df.sort_values()
方法的文档 here
如果你的数据结构是dataframe,你可以使用sort函数:
df.sort(['Importance'],ascending=True) or
df.sort(['Importance'],ascending=False)
"True" 或 "False" 取决于您的选择,这意味着您的数据按降序或升序列出。
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
我想重新塑造 df,根据 Importance
对行进行排名,我该怎么做?
我试过了
groupby(['Importance'],as_index=False)
但是不行 谢谢
使用sort_values函数:
test = df.sort_values('Importance')
假设 df
是数据帧
df.groupby()
如果您想对数据进行聚合,则可以使用,您要查找的是 df.sort_values()
.
使用 df.sort_values()
,您传入 by
字符串告诉 pandas 要排序的列。
对于您的代码,我希望 df.sort_values(by='Importance')
你可以把这个结果赋给一个新的dataframe,或者传入inplace=true
参数对df进行排序。
您可以查看 df.sort_values()
方法的文档 here
如果你的数据结构是dataframe,你可以使用sort函数:
df.sort(['Importance'],ascending=True) or
df.sort(['Importance'],ascending=False)
"True" 或 "False" 取决于您的选择,这意味着您的数据按降序或升序列出。