如何在Pandas行中使用排名替换值

3
我有一个数据框(DataFrame),其中包含多列,每一列都有它们自己的值。
data = {'name' : ['bill', 'joe', 'steve'],
    'test1' : [7, 75, 85],
    'test2' : [35, 45, 83],
     'test3' : [51, 61, 45]}
df = pd.DataFrame(data)

    name  test1  test2  test3
0   bill      7     35     51
1    joe     75     45     61
2  steve     85     83     45

我希望能替换一些列中的值,使用相对排名而不是实际值。输出结果如下所示。
    name  test1  test2  test3
0   bill      3     2      1
1    joe      1     3      2
2  steve      1     2      3

有没有一种方法可以做到这一点?


你从哪里获取相对值?在你的例子中,它们似乎不一致。更新:我明白了,较低的值得到3,较高的值得到1。 - delimiter
3个回答

7
您可以在轴1上使用DataFrame.rank
df = df.assign(**df.iloc[:, 1:].rank(axis = 1, ascending = False).astype(int))

    name    test1   test2   test3
0   bill    3       2       1
1   joe     1       3       2
2   steve   1       2       3

1
我喜欢双星号(-: - piRSquared

3

令人神智颠倒的Numpy作业

a = np.argsort(df.iloc[:, 1:].to_numpy(), axis=1)
n, m = a.shape
b = np.empty_like(a)
c, d = np.mgrid[:n, :m]
b[c, a] = m - d

df.iloc[:, 1:] = b

df

    name  test1  test2  test3
0   bill      3      2      1
1    joe      1      3      2
2  steve      1      2      3

这实际上是一个比被接受的答案更好的答案,因为它甚至处理了几个值共享相同排名的情况。 - Serge de Gosson de Varennes

2
>>> df.rank("columns", ascending=False)
   test1  test2  test3
0    3.0    2.0    1.0
1    1.0    3.0    2.0
2    1.0    2.0    3.0

>>> rankcols = ["test1", "test2", "test3"]
>>> df[rankcols] = df[rankcols].rank("columns", ascending=False).astype(int)
>>> df
    name  test1  test2  test3
0   bill      3      2      1
1    joe      1      3      2
2  steve      1      2      3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接