Transform or rank nested values
A dataframe has multiple values collected into one list per row, and each value needs ranking, casting, string matching, or another Polars expression.
Polars · Module
Applies a Polars expression to every element of each list and returns the results as lists. Use pl.element() to reference the current element; optional inner parallelism is available but should not be enabled indiscriminately.
Use this expression namespace method when each row contains a variable-length list and an expression must be applied independently to every element. Reference the current element with pl.element(); the result remains a list-valued expression rather than being automatically unwrapped.
Expr.list.eval(expr: Expr, *, parallel: bool = False) → Expr| Name | Type | Description |
|---|---|---|
expr | Expr | Expression to run. Use pl.element() to reference each list item. |
parallel | bool = False | Whether to run expression evaluation in parallel; defaults to False. |
Expr
import polars as pl
df = pl.DataFrame({"a": [1, 8, 3], "b": [4, 5, 2]})
result = df.with_columns(
pl.concat_list("a", "b")
.list.eval(pl.element().rank())
.alias("rank")
)
print(result)Ranks the values in each row's constructed list.
A dataframe has multiple values collected into one list per row, and each value needs ranking, casting, string matching, or another Polars expression.
A list column requires per-element validation or normalization while retaining one list output per input row.
A computation must run over variable-length lists in expression context rather than fixed-size arrays.
It applies a Polars expression to every element of each list and returns an Expr. Inside the expression, pl.element() identifies the current list item.
Follow-up: How would you obtain scalar aggregation results instead? Use list.agg, which evaluates an expression and automatically explodes suitable single-value results.
The default is parallel=False. parallel=True requests parallel evaluation across list elements, but it should be used only when each element computation supplies enough work to amortize threading overhead.
Follow-up: Why can this be counterproductive in group-by code? Group execution is already parallelized, so adding inner parallelism can create unnecessary nested parallel work.
The method is for the List datatype, whose lists can vary in length. For fixed-size Array values, use the corresponding arr.eval method instead.
Follow-up: What related method preserves list results while agg may unwrap them? list.eval always returns a list, whereas list.agg can automatically explode a statically single-valued result.
polars.Expr.list.aggpolars.Expr.arr.evalpolars.elementpolars.Expr.list