+1 vote
in Programming Languages by (72.7k points)
Is there any pandas method to fetch the first k rows with the largest values in the given column(s) of a dataframe?

1 Answer

+1 vote
by (354k points)
edited by
 
Best answer

You can use the nlargest() method of pandas. This method returns the first k rows with the largest values in the specified column(s) in descending order. It also returns columns that are not specified, but those columns will not be ordered in the result. 

Here is an example:

>>> df=pd.DataFrame({'a':np.random.randint(1,100,11), 'b':np.random.randint(1,100,11),'c':np.random.randint(1,100,11)})

>>> df

     a   b   c

0   94  63  55

1   54  83   5

2   11  84   6

3   98  34  42

4   57  76  32

5   38  44  97

6   65  16  99

7   97  90  58

8   13  52   6

9   29  95  75

10  63  92  79

>>> df.nlargest(3,['a'])

    a   b   c

3  98  34  42

7  97  90  58

0  94  63  55

>>> df.nlargest(3,['a','b'])

    a   b   c

3  98  34  42

7  97  90  58

0  94  63  55

Another approach using sort_values() method:

>>> df.sort_values('a', ascending=False).head(3)

    a   b   c

3  98  34  42

7  97  90  58

0  94  63  55


...