pandas.core.groupby.GroupBy.tail
- GroupBy.tail(n=5)[source]
-
Return last n rows of each group.
Similar to
.apply(lambda x: x.tail(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([['a', 1], ['a', 2], ['b', 1], ['b', 2]], ... columns=['A', 'B']) >>> df.groupby('A').tail(1) A B 1 a 2 3 b 2 >>> df.groupby('A').tail(-1) Empty DataFrame Columns: [A, B] Index: []
© 2008–2021, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/1.3.4/reference/api/pandas.core.groupby.GroupBy.tail.html