snowflake.snowpark.modin.plugin.extensions.groupby_overrides.DataFrameGroupBy.tail

DataFrameGroupBy.tail(n=5)[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.26.0/snowpark-python/src/snowflake/snowpark/modin/plugin/extensions/groupby_overrides.py#L965-L985)

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).

Parameters:

n (int) – If positive: number of entries to include from the end of each group. If negative: number of entries to exclude from the start of each group.

Returns:

Subset of the original Series or DataFrame as determined by n.

Return type:

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)
   A  B
1  a  2
3  b  2
Copy
>>> df = pd.DataFrame(
...     {
...         "col1": ["Z", None, "X", "Z", "Y", "X", "X", None, "X", "Y"],
...         "col2": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
...         "col3": [40, 50, 60, 10, 20, 30, 40, 80, 90, 10],
...         "col4": [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
...     },
...     index=list("abcdefghij"),
... )
>>> df
   col1  col2  col3  col4
a     Z     1    40    -1
b  None     2    50    -2
c     X     3    60    -3
d     Z     4    10    -4
e     Y     5    20    -5
f     X     6    30    -6
g     X     7    40    -7
h  None     8    80    -8
i     X     9    90    -9
j     Y    10    10   -10
>>> df.groupby("col1", dropna=False).tail(2)
   col1  col2  col3  col4
a     Z     1    40    -1
b  None     2    50    -2
d     Z     4    10    -4
e     Y     5    20    -5
g     X     7    40    -7
h  None     8    80    -8
i     X     9    90    -9
j     Y    10    10   -10
>>> df.groupby("col1", dropna=False).tail(-2)
  col1  col2  col3  col4
g    X     7    40    -7
i    X     9    90    -9
Copy
Language: English