Skip to content

Run any polars expression on the list values

Source code

Description

Run any polars expression on the list values

Usage

<Expr>$list$eval(expr, parallel = FALSE)

Arguments

expr Expression to run. Note that you can select an element with pl$element(), pl$first(), and more. See Examples.
parallel Run all expression parallel. Don’t activate this blindly. Parallelism is worth it if there is enough work to do per thread. This likely should not be used in the $group_by() context, because we already do parallel execution per group.

Value

Expr

Examples

library("polars")

df = pl$DataFrame(
  a = list(c(1, 8, 3), c(3, 2), c(NA, NA, 1)),
  b = list(c("R", "is", "amazing"), c("foo", "bar"), "text")
)

df
#> shape: (3, 2)
#> ┌───────────────────┬────────────────────────┐
#> │ a                 ┆ b                      │
#> │ ---               ┆ ---                    │
#> │ list[f64]         ┆ list[str]              │
#> ╞═══════════════════╪════════════════════════╡
#> │ [1.0, 8.0, 3.0]   ┆ ["R", "is", "amazing"] │
#> │ [3.0, 2.0]        ┆ ["foo", "bar"]         │
#> │ [null, null, 1.0] ┆ ["text"]               │
#> └───────────────────┴────────────────────────┘
# standardize each value inside a list, using only the values in this list
df$select(
  a_stand = pl$col("a")$list$eval(
    (pl$element() - pl$element()$mean()) / pl$element()$std()
  )
)
#> shape: (3, 1)
#> ┌──────────────────────────────┐
#> │ a_stand                      │
#> │ ---                          │
#> │ list[f64]                    │
#> ╞══════════════════════════════╡
#> │ [-0.83205, 1.1094, -0.27735] │
#> │ [0.707107, -0.707107]        │
#> │ [null, null, null]           │
#> └──────────────────────────────┘
# count characters for each element in list. Since column "b" is list[str],
# we can apply all `$str` functions on elements in the list:
df$select(
  b_len_chars = pl$col("b")$list$eval(
    pl$element()$str$len_chars()
  )
)
#> shape: (3, 1)
#> ┌─────────────┐
#> │ b_len_chars │
#> │ ---         │
#> │ list[u32]   │
#> ╞═════════════╡
#> │ [1, 2, 7]   │
#> │ [3, 3]      │
#> │ [4]         │
#> └─────────────┘
# concat strings in each list
df$select(
  pl$col("b")$list$eval(pl$element()$str$join(" "))$list$first()
)
#> shape: (3, 1)
#> ┌──────────────┐
#> │ b            │
#> │ ---          │
#> │ str          │
#> ╞══════════════╡
#> │ R is amazing │
#> │ foo bar      │
#> │ text         │
#> └──────────────┘