Skip to content

Remove rows, dropping those that match the given predicate expression(s)

Source code

Description

The original order of the remaining rows is preserved. Rows where the filter does not evaluate to TRUE are retained (this includes rows where the predicate evaluates as null).

Usage

<DataFrame>$remove(...)

Arguments

\<dynamic-dots\> Expression that evaluates to a boolean Series.

Value

A polars DataFrame

Examples

library("polars")

df <- pl$DataFrame(
  ccy = c("USD", "EUR", "USD", "JPY"),
  year = c(2021, 2022, 2023, 2023),
  total = c(3245, NA, -6680, 25000),
)

# Remove rows matching a condition. Note that the row where `total` is null
# is kept:
df$remove(pl$col("total") >= 0)
#> shape: (2, 3)
#> ┌─────┬────────┬─────────┐
#> │ ccy ┆ year   ┆ total   │
#> │ --- ┆ ---    ┆ ---     │
#> │ str ┆ f64    ┆ f64     │
#> ╞═════╪════════╪═════════╡
#> │ EUR ┆ 2022.0 ┆ null    │
#> │ USD ┆ 2023.0 ┆ -6680.0 │
#> └─────┴────────┴─────────┘
# Note that this is *not* the same as simply inverting the condition in
# `$filter()` because `$filter()` doesn't keep predicates that evaluate to
# null:
df$filter(pl$col("total") < 0)
#> shape: (1, 3)
#> ┌─────┬────────┬─────────┐
#> │ ccy ┆ year   ┆ total   │
#> │ --- ┆ ---    ┆ ---     │
#> │ str ┆ f64    ┆ f64     │
#> ╞═════╪════════╪═════════╡
#> │ USD ┆ 2023.0 ┆ -6680.0 │
#> └─────┴────────┴─────────┘
# We can use multiple conditions, combined with and/or operators:
df$remove((pl$col("total") >= 0) & (pl$col("ccy") == "USD"))
#> shape: (3, 3)
#> ┌─────┬────────┬─────────┐
#> │ ccy ┆ year   ┆ total   │
#> │ --- ┆ ---    ┆ ---     │
#> │ str ┆ f64    ┆ f64     │
#> ╞═════╪════════╪═════════╡
#> │ EUR ┆ 2022.0 ┆ null    │
#> │ USD ┆ 2023.0 ┆ -6680.0 │
#> │ JPY ┆ 2023.0 ┆ 25000.0 │
#> └─────┴────────┴─────────┘
df$remove((pl$col("total") >= 0) | (pl$col("ccy") == "USD"))
#> shape: (1, 3)
#> ┌─────┬────────┬───────┐
#> │ ccy ┆ year   ┆ total │
#> │ --- ┆ ---    ┆ ---   │
#> │ str ┆ f64    ┆ f64   │
#> ╞═════╪════════╪═══════╡
#> │ EUR ┆ 2022.0 ┆ null  │
#> └─────┴────────┴───────┘