polars.lazyframe.group_by.LazyGroupBy.last#

LazyGroupBy.last(*, ignore_nulls: bool = False) LazyFrame[source]#

Aggregate the last values in the group.

Parameters:
ignore_nulls

Ignore null values (default False). If set to True, the last non-null value for each aggregation is returned, otherwise None is returned if no non-null value exists.

Examples

>>> ldf = pl.DataFrame(
...     {
...         "a": [1, 2, 2, 3, 4, 5],
...         "b": [0.5, 0.5, 4, 10, 14, 13],
...         "c": [True, True, False, None, False, True],
...         "d": ["Apple", "Orange", "Apple", "Apple", "Banana", "Banana"],
...     }
... ).lazy()
>>> ldf.group_by("d", maintain_order=True).last().collect()
shape: (3, 4)
┌────────┬─────┬──────┬──────┐
│ d      ┆ a   ┆ b    ┆ c    │
│ ---    ┆ --- ┆ ---  ┆ ---  │
│ str    ┆ i64 ┆ f64  ┆ bool │
╞════════╪═════╪══════╪══════╡
│ Apple  ┆ 3   ┆ 10.0 ┆ null │
│ Orange ┆ 2   ┆ 0.5  ┆ true │
│ Banana ┆ 5   ┆ 13.0 ┆ true │
└────────┴─────┴──────┴──────┘
>>> ldf.group_by("d", maintain_order=True).last(ignore_nulls=True).collect()
shape: (3, 4)
┌────────┬─────┬──────┬───────┐
│ d      ┆ a   ┆ b    ┆ c     │
│ ---    ┆ --- ┆ ---  ┆ ---   │
│ str    ┆ i64 ┆ f64  ┆ bool  │
╞════════╪═════╪══════╪═══════╡
│ Apple  ┆ 3   ┆ 10.0 ┆ false │
│ Orange ┆ 2   ┆ 0.5  ┆ true  │
│ Banana ┆ 5   ┆ 13.0 ┆ true  │
└────────┴─────┴──────┴───────┘