pandas.Series.nlargest

Return the largest n elements.

param n
int, default 5

Return this many descending sorted values.

param keep
{‘first’, ‘last’, ‘all’}, default ‘first’

When there are duplicate values that cannot all fit in a Series of n elements:

  • firstreturn the first n occurrences in order

    of appearance.

  • lastreturn the last n occurrences in reverse

    order of appearance.

  • allkeep all occurrences. This can result in a Series of

    size larger than n.

return

Series The n largest values in the Series, sorted in decreasing order.

Limitations

  • Parameter keep is supported only with default value 'first'.

  • This function may reveal slower performance than Pandas* on user system. Users should exercise a tradeoff

    between staying in JIT-region with that function or going back to interpreter mode.

Examples

Returns the largest n elements.
import numpy as np
import pandas as pd
from numba import njit


@njit
def series_nlargest():
    series = pd.Series(np.arange(10))

    return series.nlargest(4)  # Expect series of 9, 8, 7, 6


print(series_nlargest())
$ python ./series/series_nlargest.py
9    9
8    8
7    7
6    6
dtype: int64

See also

Series.nsmallest

Get the n smallest elements.

Series.sort_values

Sort Series by values.

Series.head

Return the first n rows.