Add or overwrite fields of this struct
Description
This is similar to with_columns()
on DataFrame and
LazyFrame.
Usage
<Expr>$struct$with_fields(...)
Arguments
…
|
\<dynamic-dots \> Field(s) to add. Accepts expression input.
Strings are parsed as column names, other non-expression inputs are
parsed as literals.
|
Value
A polars expression
Examples
library("polars")
df <- pl$DataFrame(
x = c(1, 4, 9),
y = c(4, 9, 16),
multiply = c(10, 2, 3)
)$select(coords = pl$struct("x", "y"), "multiply")
df
#> shape: (3, 2)
#> ┌────────────┬──────────┐
#> │ coords ┆ multiply │
#> │ --- ┆ --- │
#> │ struct[2] ┆ f64 │
#> ╞════════════╪══════════╡
#> │ {1.0,4.0} ┆ 10.0 │
#> │ {4.0,9.0} ┆ 2.0 │
#> │ {9.0,16.0} ┆ 3.0 │
#> └────────────┴──────────┘
df <- df$with_columns(
pl$col("coords")$struct$with_fields(
pl$field("x")$sqrt(),
y_mul = pl$field("y") * pl$col("multiply")
)
)
df
#> shape: (3, 2)
#> ┌─────────────────┬──────────┐
#> │ coords ┆ multiply │
#> │ --- ┆ --- │
#> │ struct[3] ┆ f64 │
#> ╞═════════════════╪══════════╡
#> │ {1.0,4.0,40.0} ┆ 10.0 │
#> │ {2.0,9.0,18.0} ┆ 2.0 │
#> │ {3.0,16.0,48.0} ┆ 3.0 │
#> └─────────────────┴──────────┘
#> shape: (3, 3)
#> ┌─────┬──────┬───────┐
#> │ x ┆ y ┆ y_mul │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ f64 │
#> ╞═════╪══════╪═══════╡
#> │ 1.0 ┆ 4.0 ┆ 40.0 │
#> │ 2.0 ┆ 9.0 ┆ 18.0 │
#> │ 3.0 ┆ 16.0 ┆ 48.0 │
#> └─────┴──────┴───────┘