polars.concat_list#

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

Horizontally concatenate columns into a single list column.

Operates in linear time.

Parameters:
exprs

Columns to concatenate into a single list column. Accepts expression input. Strings are parsed as column names, other non-expression inputs are parsed as literals.

*more_exprs

Additional columns to concatenate into a single list column, specified as positional arguments.

Examples

Create lagged columns and collect them into a list. This mimics a rolling window.

>>> df = pl.DataFrame({"A": [1.0, 2.0, 9.0, 2.0, 13.0]})
>>> df = df.select([pl.col("A").shift(i).alias(f"A_lag_{i}") for i in range(3)])
>>> df.select(
...     pl.concat_list([f"A_lag_{i}" for i in range(3)][::-1]).alias("A_rolling")
... )
shape: (5, 1)
┌───────────────────┐
│ A_rolling         │
│ ---               │
│ list[f64]         │
╞═══════════════════╡
│ [null, null, 1.0] │
│ [null, 1.0, 2.0]  │
│ [1.0, 2.0, 9.0]   │
│ [2.0, 9.0, 2.0]   │
│ [9.0, 2.0, 13.0]  │
└───────────────────┘