polars.coalesce#

polars.coalesce(exprs: IntoExpr | Iterable[IntoExpr], *more_exprs: IntoExpr) Expr[source]#

Folds the columns from left to right, keeping the first non-null value.

Parameters:
exprs

Columns to coalesce. Accepts expression input. Strings are parsed as column names, other non-expression inputs are parsed as literals.

*more_exprs

Additional columns to coalesce, specified as positional arguments.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, None, None, None],
...         "b": [1, 2, None, None],
...         "c": [5, None, 3, None],
...     }
... )
>>> df.with_columns(pl.coalesce(["a", "b", "c", 10]).alias("d"))
shape: (4, 4)
┌──────┬──────┬──────┬─────┐
│ a    ┆ b    ┆ c    ┆ d   │
│ ---  ┆ ---  ┆ ---  ┆ --- │
│ i64  ┆ i64  ┆ i64  ┆ i64 │
╞══════╪══════╪══════╪═════╡
│ 1    ┆ 1    ┆ 5    ┆ 1   │
│ null ┆ 2    ┆ null ┆ 2   │
│ null ┆ null ┆ 3    ┆ 3   │
│ null ┆ null ┆ null ┆ 10  │
└──────┴──────┴──────┴─────┘
>>> df.with_columns(pl.coalesce(pl.col(["a", "b", "c"]), 10.0).alias("d"))
shape: (4, 4)
┌──────┬──────┬──────┬──────┐
│ a    ┆ b    ┆ c    ┆ d    │
│ ---  ┆ ---  ┆ ---  ┆ ---  │
│ i64  ┆ i64  ┆ i64  ┆ f64  │
╞══════╪══════╪══════╪══════╡
│ 1    ┆ 1    ┆ 5    ┆ 1.0  │
│ null ┆ 2    ┆ null ┆ 2.0  │
│ null ┆ null ┆ 3    ┆ 3.0  │
│ null ┆ null ┆ null ┆ 10.0 │
└──────┴──────┴──────┴──────┘