polars.lazyframe.group_by.LazyGroupBy.first#

LazyGroupBy.first() LazyFrame[source]#

Aggregate the first values in the group.

Examples

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