Skip to content

Return the sum per group

Description

Return the sum per group

Usage

<GroupBy>$sum()

Value

A polars DataFrame

Examples

library("polars")

df <- pl$DataFrame(
  grp = c("c", "c", "a", "c", "a", "b"),
  x = c(0.5, 0.5, 4, 10, 13, 14),
  y = 1:6,
  z = c(TRUE, TRUE, FALSE, TRUE, FALSE, TRUE)
)
df
#> shape: (6, 4)
#> ┌─────┬──────┬─────┬───────┐
#> │ grp ┆ x    ┆ y   ┆ z     │
#> │ --- ┆ ---  ┆ --- ┆ ---   │
#> │ str ┆ f64  ┆ i32 ┆ bool  │
#> ╞═════╪══════╪═════╪═══════╡
#> │ c   ┆ 0.5  ┆ 1   ┆ true  │
#> │ c   ┆ 0.5  ┆ 2   ┆ true  │
#> │ a   ┆ 4.0  ┆ 3   ┆ false │
#> │ c   ┆ 10.0 ┆ 4   ┆ true  │
#> │ a   ┆ 13.0 ┆ 5   ┆ false │
#> │ b   ┆ 14.0 ┆ 6   ┆ true  │
#> └─────┴──────┴─────┴───────┘
df$group_by("grp")$sum()
#> shape: (3, 4)
#> ┌─────┬──────┬─────┬─────┐
#> │ grp ┆ x    ┆ y   ┆ z   │
#> │ --- ┆ ---  ┆ --- ┆ --- │
#> │ str ┆ f64  ┆ i32 ┆ u32 │
#> ╞═════╪══════╪═════╪═════╡
#> │ c   ┆ 11.0 ┆ 7   ┆ 3   │
#> │ b   ┆ 14.0 ┆ 6   ┆ 1   │
#> │ a   ┆ 17.0 ┆ 8   ┆ 0   │
#> └─────┴──────┴─────┴─────┘