擅长:python、mysql、java
<p>您可以<a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rank.html" rel="nofollow noreferrer">^{<cd1>}</a>,然后通过<a href="https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.where.html" rel="nofollow noreferrer">^{<cd2>}</a>有条件地赋值:</p>
<pre><code>df[:] = np.where(df.rank(axis=1) > df.shape[1] / 2, 1, -1)
print(df)
A B
0 1 -1
1 -1 1
2 -1 1
3 1 -1
</code></pre>
<p><strong>注意:</strong>这假设重复的值总是得到相同的秩。你知道吗</p>