簡體   English   中英

查找Pandas中軸上N個最大值的索引

[英]Finding the indexes of the N maximum values across an axis in Pandas

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

但是,如果我們想要獲得軸上10個最高值的索引呢?

怎么可以實現呢?

例如:

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

例如,IIUC,如果你想獲得列col的前10個最大數字的索引:

data[col].nlargest(10).index

你可以使用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

試一試。 這將獲取一行中的10個最大值並將它們放入數據幀中。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.random_sample((50, 40)))
df2 = pd.DataFrame(np.sort(df.values)[:,-10:])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM