在 Pandas 中查找跨轴的 N 个最大值的索引

use*_*077 5 python pandas argmax

我知道有一个方法 .argmax() 返回轴上最大值的索引。

但是,如果我们想要获取某个轴上 10 个最高值的索引该怎么办?

这怎么可能实现呢?

例如:

data = pd.DataFrame(np.random.random_sample((50, 40)))
Run Code Online (Sandbox Code Playgroud)

gmd*_*mds 0

您可以使用argsort

s = pd.Series(np.random.permutation(30))
sorted_indices = s.argsort()
top_10 = sorted_indices[sorted_indices < 10]
print(top_10)
Run Code Online (Sandbox Code Playgroud)

输出:

3     9
4     1
6     0
8     7
13    4
14    2
15    3
19    8
20    5
24    6
dtype: int64
Run Code Online (Sandbox Code Playgroud)