Skip to main content
`Ctrl`+`K`

# pandas.api.typing.SeriesGroupBy.nsmallest

SeriesGroupBy.nsmallest(n=5, keep='first')

  Return the smallest n elements.

  Parameters:
    n int, default 5

      Return this many ascending sorted values.

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

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

      - `first` : return the first n occurrences in order of appearance.

      - `last` : return the last n occurrences in reverse order of appearance.

      - `all` : keep all occurrences. This can result in a Series of size larger than n.

  Returns:
    Series

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

  See also

  `Series.nlargest`

    Get the n largest elements.

  `Series.sort_values`

    Sort Series by values.

  `Series.head`

    Return the first n rows.

  Notes

  Faster than `.sort_values().head(n)` for small n relative to the size of the `Series` object.

  Examples

      >>> s = pd.Series([1, 2, 3, 4, 5, 6])

      >>> s
      0    1
      1    2
      2    3
      3    4
      4    5
      5    6
      dtype: int64

      >>> s.groupby([1, 1, 1, 2, 2, 2]).nsmallest(n=2)
      1  0    1
         1    2
      2  3    4
         4    5
      dtype: int64