Build composite labels
Create readable row-level labels from IDs, names, or categories.
Polars · Function
Concatenates multiple columns or expressions horizontally into one string column. It can insert a separator, casts non-string inputs to String, and optionally skips null values.
Use pl.concat_str to combine several expressions horizontally into one string expression. Inputs are converted to strings when needed, and null handling is controlled by ignore_nulls.
polars.concat_str(exprs: IntoExpr | Iterable[IntoExpr], *more_exprs: IntoExpr, separator: str = '', ignore_nulls: bool = False) → Expr| Name | Type | Description |
|---|---|---|
exprs | IntoExpr | Iterable[IntoExpr] | 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 | IntoExpr | Additional columns to concatenate into a single string column, specified as positional arguments. |
separator | str = '' | String used to separate the values of each column. |
ignore_nulls | bool = False | Whether to ignore null values; the default is False. |
Expr
import polars as pl
df = pl.DataFrame({"a": [1, 2, 3], "b": ["dogs", "cats", None], "c": ["play", "swim", "walk"]})
result = df.with_columns(
pl.concat_str(
[pl.col("a") * 2, pl.col("b"), pl.col("c")],
separator=" ",
).alias("full_sentence")
)
print(result)Builds one string expression per row from transformed and existing columns, separating values with spaces.
Create readable row-level labels from IDs, names, or categories.
Combine transformed numeric and text expressions into one sentence-like field.
Assemble path, key, or identifier components with a chosen delimiter.
It concatenates columns horizontally, producing one string value for each row. The operation runs in linear time.
Follow-up: How do numeric inputs behave? They are cast to String before concatenation.
exprs is the primary iterable or expression input, while more_exprs accepts additional columns as positional arguments. Strings are interpreted as column names and other non-expression values as literals.
Follow-up: How would you include a fixed prefix? Pass a literal expression such as pl.lit("id-") alongside the column expressions.
separator is inserted between values from each input expression. By default it is an empty string, so concatenation occurs without a delimiter.
Follow-up: What happens with a null when ignore_nulls is true? The null value is skipped rather than propagating to the row result.
polars.concat_listpolars.concatpolars.Expr.str.concat