Skip to content

Add two expressions

Source code

Description

Method equivalent of addition operator expr + other.

Usage

<Expr>$add(other)

Arguments

other numeric or string value; accepts expression input.

Value

Expr

See Also

  • Arithmetic operators

Examples

library("polars")

df = pl$DataFrame(x = 1:5)

df$with_columns(
  `x+int` = pl$col("x")$add(2L),
  `x+expr` = pl$col("x")$add(pl$col("x")$cum_prod())
)
#> shape: (5, 3)
#> ┌─────┬───────┬────────┐
#> │ x   ┆ x+int ┆ x+expr │
#> │ --- ┆ ---   ┆ ---    │
#> │ i32 ┆ i32   ┆ i64    │
#> ╞═════╪═══════╪════════╡
#> │ 1   ┆ 3     ┆ 2      │
#> │ 2   ┆ 4     ┆ 4      │
#> │ 3   ┆ 5     ┆ 9      │
#> │ 4   ┆ 6     ┆ 28     │
#> │ 5   ┆ 7     ┆ 125    │
#> └─────┴───────┴────────┘
df = pl$DataFrame(
  x = c("a", "d", "g"),
  y = c("b", "e", "h"),
  z = c("c", "f", "i")
)

df$with_columns(
  pl$col("x")$add(pl$col("y"))$add(pl$col("z"))$alias("xyz")
)
#> shape: (3, 4)
#> ┌─────┬─────┬─────┬─────┐
#> │ x   ┆ y   ┆ z   ┆ xyz │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ str ┆ str ┆ str ┆ str │
#> ╞═════╪═════╪═════╪═════╡
#> │ a   ┆ b   ┆ c   ┆ abc │
#> │ d   ┆ e   ┆ f   ┆ def │
#> │ g   ┆ h   ┆ i   ┆ ghi │
#> └─────┴─────┴─────┴─────┘