python - 如何对 pandas 中每一行的值进行排名

标签 python pandas

我想对 pandas 中每一行的值进行排名,那么该怎么做呢? 数据如下:

   a            b            c          
0.000121    -0.000034   -0.000455   
-0.000265   -0.000007   -0.000009   

期望的结果如下:

   a            b            c         a_rank        b_rank       c_rank          
0.000121    -0.000034   -0.000455         1            2            3
-0.000265   -0.000007   -0.000009         3            1            2 

感谢您的帮助!

最佳答案

使用DataFrame.rank使用参数 axis=1ascending=False,添加 DataFrame.add_suffix对于 chnage 列名称和最后一个 DataFrame.join原文:

df = df.join(df.rank(axis=1, ascending=False).astype(int).add_suffix('_rank'))
print (df)
          a         b         c  a_rank  b_rank  c_rank
0  0.000121 -0.000034 -0.000455       1       2       3
1 -0.000265 -0.000007 -0.000009       3       1       2

关于python - 如何对 pandas 中每一行的值进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422352/

相关文章:

python - 如何在 Groovy 中设置对象的属性

python - 密码保护使用 pisa 创建的 pdf 文件

python - 计算每组每 n 天的斜率

python - 从 Numpy 数组 : How do I specify the index column and column headers? 创建 Pandas DataFrame

python - 如何设置嵌套 numpy ndarray 的 dtype?

python - 从另一个文件导入列表

python - 使用 Python 获取通过 SMS 发送的 Twitter 消息

Python pandas plot 散点日期时间错误

python - Pandas DataFrame 乘以列并创建新列

python - 如何将一列保留为数据框