<p>我对Python很陌生。
我有一个如下的数据框</p>
<pre><code> book 1 book 2 book 3
user 1 0.05 0.47 0.09
user 2 0.3 0.01 0.35
</code></pre>
<p>我希望根据值(降序)对其进行排序,输出如下:</p>
<pre><code> Rank 1 Rank 2 Rank 3
user1 book 2 book 3 book 1
user2 book 3 book 1 book 2
</code></pre>
<p><a href="https://i.stack.imgur.com/wiHpP.jpg" rel="nofollow noreferrer">this is how the matrix looks in real task</a></p>
<p>我会感激你的帮助。你知道吗</p>
<p>谢谢你。你知道吗</p>
<p>您可以使用apply:</p>
<pre><code>(
df.apply(lambda x: x.sort_values().index[::-1], axis=1)
.rename(columns=lambda x: x.replace('book','Rank'))
)
Out[9]:
Rank 1 Rank 2
user 1 book 2 book 1
user 2 book 1 book 2
</code></pre>