我知道有一种方法.argmax()返回一个轴上最大值的索引。
但是,如果我们想获取整个轴上10个最大值的索引,该怎么办?
这怎么实现?
例如:
data = pd.DataFrame(np.random.random_sample((50, 40)))
答案 0 :(得分:0)
IIUC,例如,如果要获取列col
的前10大编号的索引:
data[col].nlargest(10).index
答案 1 :(得分:0)
您可以使用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
答案 2 :(得分:0)
尝试一下。这将连续获取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:])