5

I know that there is a method .argmax() that returns the indexes of the maximum values across an axis.

But what if we want to get the indexes of the 10 highest values across an axis?

How could this be accomplished?

E.g.:

data = pd.DataFrame(np.random.random_sample((50, 40)))
halfer
  • 19,824
  • 17
  • 99
  • 186
user8270077
  • 4,621
  • 17
  • 75
  • 140
  • What is your expected output – BENY Apr 11 '19 at 22:29
  • you should provide a sample result that you want because the question can be interpenetrated differently. Is it the sum of 10 highest number in a row? or 10 highest in a column? – adhg Apr 11 '19 at 22:37

3 Answers3

1

IIUC, say, if you want to get the index of the top 10 largest numbers of column col:

data[col].nlargest(10).index
Juan C
  • 5,846
  • 2
  • 17
  • 51
0

You can use argsort:

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

Output:

3     9
4     1
6     0
8     7
13    4
14    2
15    3
19    8
20    5
24    6
dtype: int64
gmds
  • 19,325
  • 4
  • 32
  • 58
0

Give this a try. This will take the 10 largest values across a row and put them into a dataframe.

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:])
Angel Roman
  • 598
  • 1
  • 3
  • 13