pandas.core.groupby.GroupBy.head¶
- GroupBy.head(n=5)[source]¶
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).Does not work for negative values of n.
- Returns
- Series or DataFrame
See also
Series.groupby
Apply a function groupby to a Series.
DataFrame.groupby
Apply a function groupby 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) Empty DataFrame Columns: [A, B] Index: []