Skip to main content
`Ctrl`+`K`

# pandas.api.typing.DataFrameGroupBy.head

DataFrameGroupBy.head(n=5)

  Return first n rows of each group.

  Similar to `.apply(lambda x: x.head(n))`, but it returns a subset of rows from the original DataFrame with original index and order preserved (`as_index` flag is ignored).

  Parameters:
    n int

      If positive: number of entries to include from start of each group. If negative: number of entries to exclude from end of each group.

  Returns:
    Series or DataFrame

      Subset of original Series or DataFrame as determined by n.

  See also

  `Series.head`

    Apply function head to a Series.

  `DataFrame.head`

    Apply function head to each row or column of a DataFrame.

  Examples

      >>> df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"])
      >>> df.groupby("A").head(1)
         A  B
      0  1  2
      2  5  6
      >>> df.groupby("A").head(-1)
         A  B
      0  1  2