polars.concat_str#

polars.concat_str(
exprs: IntoExpr | Iterable[IntoExpr],
*more_exprs: IntoExpr,
separator: str = '',
ignore_nulls: bool = False,
) 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-String columns are cast to String.

*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.

ignore_nulls

Ignore null values (default is False).

If set to False, null values will be propagated. if the row contains any null values, the output is null.

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          │
└─────┴──────┴──────┴───────────────┘