pandas.api.typing.SeriesGroupBy.nlargest#
- SeriesGroupBy.nlargest(n=5, keep='first')[source]#
Return the largest n elements.
- Parameters:
- nint, default 5
Return this many descending 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 largest values in the Series, sorted in decreasing order.
See also
Series.nsmallestGet the n smallest elements.
Series.sort_valuesSort Series by values.
Series.headReturn the first n rows.
Notes
Faster than
.sort_values(ascending=False).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]).nlargest(n=2) 1 2 3 1 2 2 5 6 4 5 dtype: int64