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.


Series The n smallest values in the Series, sorted in increasing order.


  • 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.


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

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

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

$ python ./series/
0    0
1    1
2    2
3    3
dtype: int64

See also


Get the n largest elements.


Sort Series by values.


Return the first n rows.