Skip to main content
`Ctrl`+`K`

# pandas.api.typing.SeriesGroupBy.nlargest

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

  Return the largest n elements.

  Within each group, returns the n largest values sorted in descending order. The `keep` parameter controls how ties are handled when there are duplicate values at the boundary.

  Parameters:
    n int, 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.nsmallest`

    Get the n smallest elements.

  `Series.sort_values`

    Sort Series by values.

  `Series.head`

    Return the first n rows.

  Notes

  Faster than `.sort_values(ascending=False).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]).nlargest(n=2)
      1  2    3
         1    2
      2  5    6
         4    5
      dtype: int64