Skip to content

Exclude columns from a multi-column expression.

Description

Exclude columns from a multi-column expression.

Usage

<Expr>$exclude(...)

Arguments

The name or datatype of the column(s) to exclude. Accepts regular expression input. Regular expressions should start with ^ and end with $.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(aa = 1:2, ba = c("a", NA), cc = c(NA, 2.5))
df
#> shape: (2, 3)
#> ┌─────┬──────┬──────┐
#> │ aa  ┆ ba   ┆ cc   │
#> │ --- ┆ ---  ┆ ---  │
#> │ i32 ┆ str  ┆ f64  │
#> ╞═════╪══════╪══════╡
#> │ 1   ┆ a    ┆ null │
#> │ 2   ┆ null ┆ 2.5  │
#> └─────┴──────┴──────┘
# Exclude by column name(s):
df$select(pl$all()$exclude("ba"))
#> shape: (2, 2)
#> ┌─────┬──────┐
#> │ aa  ┆ cc   │
#> │ --- ┆ ---  │
#> │ i32 ┆ f64  │
#> ╞═════╪══════╡
#> │ 1   ┆ null │
#> │ 2   ┆ 2.5  │
#> └─────┴──────┘
# Exclude by regex, e.g. removing all columns whose names end with the
# letter "a":
df$select(pl$all()$exclude("^.*a$"))
#> shape: (2, 1)
#> ┌──────┐
#> │ cc   │
#> │ ---  │
#> │ f64  │
#> ╞══════╡
#> │ null │
#> │ 2.5  │
#> └──────┘
# Exclude by dtype(s), e.g. removing all columns of type Int64 or Float64:
df$select(pl$all()$exclude(pl$Int64, pl$Float64))
#> shape: (2, 2)
#> ┌─────┬──────┐
#> │ aa  ┆ ba   │
#> │ --- ┆ ---  │
#> │ i32 ┆ str  │
#> ╞═════╪══════╡
#> │ 1   ┆ a    │
#> │ 2   ┆ null │
#> └─────┴──────┘