pandas.api.typing.SeriesGroupBy.nsmallest#
- SeriesGroupBy.nsmallest(n=5, keep='first')[source]#
Return the smallest n elements.
- Parameters:
- nint, 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.nlargestGet the n largest elements.
Series.sort_valuesSort Series by values.
Series.headReturn the first n rows.
Notes
Faster than
.sort_values().head(n)for small n relative to the size of theSeriesobject.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