polars.LazyFrame.remove#
- LazyFrame.remove(
- *predicates: IntoExprColumn | Iterable[IntoExprColumn] | bool | list[bool] | np.ndarray[Any, Any],
- **constraints: Any,
Remove rows, dropping those that match the given predicate expression(s).
The original order of the remaining rows is preserved.
Rows where the filter predicate does not evaluate to True are retained (this includes rows where the predicate evaluates as
null
).- Parameters:
- predicates
Expression that evaluates to a boolean Series.
- constraints
Column filters; use
name = value
to filter columns using the supplied value. Each constraint behaves the same aspl.col(name).eq(value)
, and is implicitly joined with the other filter conditions using&
.
See also
Notes
If you are transitioning from Pandas, and performing filter operations based on the comparison of two or more columns, please note that in Polars any comparison involving
null
values will result in anull
result, not boolean True or False. As a result, these rows will not be removed. Ensure that null values are handled appropriately to avoid unexpected behaviour (see examples below).Examples
>>> lf = pl.LazyFrame( ... { ... "foo": [2, 3, None, 4, 0], ... "bar": [5, 6, None, None, 0], ... "ham": ["a", "b", None, "c", "d"], ... } ... )
Remove rows matching a condition:
>>> lf.remove( ... pl.col("bar") >= 5, ... ).collect() shape: (3, 3) ┌──────┬──────┬──────┐ │ foo ┆ bar ┆ ham │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ str │ ╞══════╪══════╪══════╡ │ null ┆ null ┆ null │ │ 4 ┆ null ┆ c │ │ 0 ┆ 0 ┆ d │ └──────┴──────┴──────┘
Discard rows based on multiple conditions, combined with and/or operators:
>>> lf.remove( ... (pl.col("foo") >= 0) & (pl.col("bar") >= 0), ... ).collect() shape: (2, 3) ┌──────┬──────┬──────┐ │ foo ┆ bar ┆ ham │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ str │ ╞══════╪══════╪══════╡ │ null ┆ null ┆ null │ │ 4 ┆ null ┆ c │ └──────┴──────┴──────┘
>>> lf.remove( ... (pl.col("foo") >= 0) | (pl.col("bar") >= 0), ... ).collect() shape: (1, 3) ┌──────┬──────┬──────┐ │ foo ┆ bar ┆ ham │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ str │ ╞══════╪══════╪══════╡ │ null ┆ null ┆ null │ └──────┴──────┴──────┘
Provide multiple constraints using
*args
syntax:>>> lf.remove( ... pl.col("ham").is_not_null(), ... pl.col("bar") >= 0, ... ).collect() shape: (2, 3) ┌──────┬──────┬──────┐ │ foo ┆ bar ┆ ham │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ str │ ╞══════╪══════╪══════╡ │ null ┆ null ┆ null │ │ 4 ┆ null ┆ c │ └──────┴──────┴──────┘
Provide constraints(s) using
**kwargs
syntax:>>> lf.remove(foo=0, bar=0).collect() shape: (4, 3) ┌──────┬──────┬──────┐ │ foo ┆ bar ┆ ham │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ str │ ╞══════╪══════╪══════╡ │ 2 ┆ 5 ┆ a │ │ 3 ┆ 6 ┆ b │ │ null ┆ null ┆ null │ │ 4 ┆ null ┆ c │ └──────┴──────┴──────┘
Remove rows by comparing two columns against each other; in this case, we remove rows where the two columns are not equal (using
ne_missing
to ensure that null values compare equal):>>> lf.remove( ... pl.col("foo").ne_missing(pl.col("bar")), ... ).collect() shape: (2, 3) ┌──────┬──────┬──────┐ │ foo ┆ bar ┆ ham │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ str │ ╞══════╪══════╪══════╡ │ null ┆ null ┆ null │ │ 0 ┆ 0 ┆ d │ └──────┴──────┴──────┘