pandas.Series.nsmallest

Return the smallest n elements.

param n
int, default 5

Return this many ascending 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 smallest values in the Series, sorted in increasing 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 smallest n elements.
import numpy as np
import pandas as pd
from numba import njit


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

    return series.nsmallest(4)  # Expect series of 0, 1, 2, 3


print(series_nsmallest())
$ python ./series/series_nsmallest.py
0    0
1    1
2    2
3    3
dtype: int64

See also

Series.nlargest

Get the n largest elements.

Series.sort_values

Sort Series by values.

Series.head

Return the first n rows.