polars.element#

polars.element() Expr[source]#

Alias for an element being evaluated in an eval expression.

Examples

A horizontal rank computation by taking the elements of a list

>>> df = pl.DataFrame({"a": [1, 8, 3], "b": [4, 5, 2]})
>>> df.with_column(
...     pl.concat_list(["a", "b"]).arr.eval(pl.element().rank()).alias("rank")
... )
shape: (3, 3)
┌─────┬─────┬────────────┐
│ a   ┆ b   ┆ rank       │
│ --- ┆ --- ┆ ---        │
│ i64 ┆ i64 ┆ list[f32]  │
╞═════╪═════╪════════════╡
│ 1   ┆ 4   ┆ [1.0, 2.0] │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 8   ┆ 5   ┆ [2.0, 1.0] │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 3   ┆ 2   ┆ [2.0, 1.0] │
└─────┴─────┴────────────┘

A mathematical operation on array elements

>>> df = pl.DataFrame({"a": [1, 8, 3], "b": [4, 5, 2]})
>>> df.with_column(
...     pl.concat_list(["a", "b"]).arr.eval(pl.element() * 2).alias("a_b_doubled")
... )
shape: (3, 3)
┌─────┬─────┬─────────────┐
│ a   ┆ b   ┆ a_b_doubled │
│ --- ┆ --- ┆ ---         │
│ i64 ┆ i64 ┆ list[i64]   │
╞═════╪═════╪═════════════╡
│ 1   ┆ 4   ┆ [2, 8]      │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 8   ┆ 5   ┆ [16, 10]    │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 3   ┆ 2   ┆ [6, 4]      │
└─────┴─────┴─────────────┘