Join elements of every sub-list
Description
Join all string items in a sub-list and place a separator between them.
This only works if the inner dtype is String
.
Usage
<Expr>$list$join(separator, ..., ignore_nulls = FALSE)
Arguments
separator
|
String to separate the items with. Can be an Expr. Strings are not parsed as columns. |
…
|
\<dynamic-dots \> 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 .
|
ignore_nulls
|
If FALSE (default), null values will be propagated, i.e. if
the row contains any null values, the output is null.
|
Value
A polars expression
Examples
library("polars")
df <- pl$DataFrame(
s = list(c("a", "b", "c"), c("x", "y"), c("e", NA)),
separator = c("-", "+", "/")
)
df$with_columns(
join_with_expr = pl$col("s")$list$join(pl$col("separator")),
join_with_lit = pl$col("s")$list$join(" "),
join_ignore_null = pl$col("s")$list$join(" ", ignore_nulls = TRUE)
)
#> shape: (3, 5)
#> ┌─────────────────┬───────────┬────────────────┬───────────────┬──────────────────┐
#> │ s ┆ separator ┆ join_with_expr ┆ join_with_lit ┆ join_ignore_null │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
#> │ list[str] ┆ str ┆ str ┆ str ┆ str │
#> ╞═════════════════╪═══════════╪════════════════╪═══════════════╪══════════════════╡
#> │ ["a", "b", "c"] ┆ - ┆ a-b-c ┆ a b c ┆ a b c │
#> │ ["x", "y"] ┆ + ┆ x+y ┆ x y ┆ x y │
#> │ ["e", null] ┆ / ┆ null ┆ null ┆ e │
#> └─────────────────┴───────────┴────────────────┴───────────────┴──────────────────┘