Skip to content

Join elements of an array

Source code

Description

Join all string items in a sub-array and place a separator between them. This only works on columns of type list[str].

Usage

<Expr>$arr$join(separator, ignore_nulls = FALSE)

Arguments

separator String to separate the items with. Can be an Expr. Strings are not parsed as columns.
ignore_nulls If FALSE (default), null values are propagated: if the row contains any null values, the output is null.

Value

Expr

Examples

library("polars")

df = pl$DataFrame(
  values = list(c("a", "b", "c"), c("x", "y", "z"), c("e", NA, NA)),
  separator = c("-", "+", "/"),
  schema = list(values = pl$Array(pl$String, 3))
)
df$with_columns(
  join_with_expr = pl$col("values")$arr$join(pl$col("separator")),
  join_with_lit = pl$col("values")$arr$join(" "),
  join_ignore_null = pl$col("values")$arr$join(" ", ignore_nulls = TRUE)
)
#> shape: (3, 5)
#> ┌───────────────────┬───────────┬────────────────┬───────────────┬──────────────────┐
#> │ values            ┆ separator ┆ join_with_expr ┆ join_with_lit ┆ join_ignore_null │
#> │ ---               ┆ ---       ┆ ---            ┆ ---           ┆ ---              │
#> │ array[str, 3]     ┆ str       ┆ str            ┆ str           ┆ str              │
#> ╞═══════════════════╪═══════════╪════════════════╪═══════════════╪══════════════════╡
#> │ ["a", "b", "c"]   ┆ -         ┆ a-b-c          ┆ a b c         ┆ a b c            │
#> │ ["x", "y", "z"]   ┆ +         ┆ x+y+z          ┆ x y z         ┆ x y z            │
#> │ ["e", null, null] ┆ /         ┆ null           ┆ null          ┆ e                │
#> └───────────────────┴───────────┴────────────────┴───────────────┴──────────────────┘