python - 在 Pandas 中查找轴上 N 个最大值的索引

标签 python pandas argmax

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

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

这是如何实现的?

例如:

data = pd.DataFrame(np.random.random_sample((50, 40)))

最佳答案

您可以使用argsort:

s = pd.Series(np.random.permutation(30))
sorted_indices = s.argsort()
top_10 = sorted_indices[sorted_indices < 10]
print(top_10)

输出:

3     9
4     1
6     0
8     7
13    4
14    2
15    3
19    8
20    5
24    6
dtype: int64

关于python - 在 Pandas 中查找轴上 N 个最大值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55642036/

相关文章:

python - 文件未在 python 中发布

excel - 使用 df.to_csv() 和 'utf-8' 将希伯来语写成 excel 出现奇怪的字符

python - Pandas idxmax : return all rows in case of ties

python - 无法使用 pytest-mock 模拟 'os.path.join'

python - 无法从python访问mysql数据库

Python - 如何根据现有列中具有相应值的唯一值在数据框中创建新列?

Python从数据帧中提取行,其中数据位于另一个数据帧的两行之间

arrays - BigQuery 参数最大值 : Is array order maintained when doing CROSS JOIN UNNEST

f# - F# 库是否具有 `argMax` 的标准函数?

python - Python 如何提供一种可维护的方式来在系统中传递数据结构?