polars.Expr.max_by#

Expr.max_by(by: IntoExpr) Expr[source]#

Get maximum value, ordered by another expression.

If the by expression has multiple values equal to the maximum it is not defined which value will be chosen.

Warning

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Parameters:
by

Column used to determine the largest element. Accepts expression input. Strings are parsed as column names.

Examples

>>> df = pl.DataFrame({"a": [-1.0, float("nan"), 1.0], "b": ["x", "y", "z"]})
>>> df.select(pl.col("b").max_by("a"))
shape: (1, 1)
┌─────┐
│ b   │
│ --- │
│ str │
╞═════╡
│ z   │
└─────┘