polars.concat_str#

polars.concat_str(
exprs: IntoExpr | Iterable[IntoExpr],
*more_exprs: IntoExpr,
separator: str = '',
) Expr[source]#

Horizontally concatenate columns into a single string column.

Operates in linear time.

Parameters:
exprs

Columns to concatenate into a single string column. Accepts expression input. Strings are parsed as column names, other non-expression inputs are parsed as literals. Non-Utf8 columns are cast to Utf8.

*more_exprs

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

separator

String that will be used to separate the values of each column.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 2, 3],
...         "b": ["dogs", "cats", None],
...         "c": ["play", "swim", "walk"],
...     }
... )
>>> df.with_columns(
...     pl.concat_str(
...         [
...             pl.col("a") * 2,
...             pl.col("b"),
...             pl.col("c"),
...         ],
...         separator=" ",
...     ).alias("full_sentence"),
... )
shape: (3, 4)
┌─────┬──────┬──────┬───────────────┐
│ a   ┆ b    ┆ c    ┆ full_sentence │
│ --- ┆ ---  ┆ ---  ┆ ---           │
│ i64 ┆ str  ┆ str  ┆ str           │
╞═════╪══════╪══════╪═══════════════╡
│ 1   ┆ dogs ┆ play ┆ 2 dogs play   │
│ 2   ┆ cats ┆ swim ┆ 4 cats swim   │
│ 3   ┆ null ┆ walk ┆ null          │
└─────┴──────┴──────┴───────────────┘