polars.dataframe.group_by.GroupBy.last#
- GroupBy.last(*, ignore_nulls: bool = False) DataFrame[source]#
Aggregate the last values in the group.
- Parameters:
- ignore_nulls
Ignore null values (default
False). If set toTrue, the last non-null value for each column is returned, otherwiseNoneis returned if no non-null value exists.
Examples
>>> df = pl.DataFrame( ... { ... "a": [1, 2, 2, 3, 4, 5], ... "b": [0.5, 0.5, 4, 10, 14, None], ... "c": [True, True, True, None, False, True], ... "d": ["Apple", "Orange", "Apple", "Apple", "Banana", "Banana"], ... } ... ) >>> df.group_by("d", maintain_order=True).last() shape: (3, 4) ┌────────┬─────┬──────┬──────┐ │ d ┆ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ f64 ┆ bool │ ╞════════╪═════╪══════╪══════╡ │ Apple ┆ 3 ┆ 10.0 ┆ null │ │ Orange ┆ 2 ┆ 0.5 ┆ true │ │ Banana ┆ 5 ┆ null ┆ true │ └────────┴─────┴──────┴──────┘ >>> df.group_by("d", maintain_order=True).last(ignore_nulls=True) shape: (3, 4) ┌────────┬─────┬──────┬──────┐ │ d ┆ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ f64 ┆ bool │ ╞════════╪═════╪══════╪══════╡ │ Apple ┆ 3 ┆ 10.0 ┆ true │ │ Orange ┆ 2 ┆ 0.5 ┆ true │ │ Banana ┆ 5 ┆ 14.0 ┆ true │ └────────┴─────┴──────┴──────┘