Row selection
Keep records meeting one or more column-based conditions.
Polars · Module
Filters a DataFrame's rows using boolean expressions or keyword equality constraints. Only rows whose conditions evaluate to true remain, in their original order; false and null results are discarded.
Call DataFrame.filter with one or more expressions that produce boolean values per row. Polars retains only rows whose combined predicate is true, preserves their original order, and also supports keyword constraints that are treated as equality checks and combined with logical AND.
DataFrame.filter(predicates: IntoExprColumn | Iterable[IntoExprColumn] | bool | list[bool] | np.ndarray[Any, Any], **constraints: Any) -> DataFrame| Name | Type | Description |
|---|---|---|
predicates | IntoExprColumn | Iterable[IntoExprColumn] | bool | list[bool] | np.ndarray[Any, Any] | Expression(s) that evaluate to a boolean Series. |
constraints | Any | Column filters written as name=value; each is an equality condition and constraints are combined with logical AND. |
DataFrame containing only rows whose filter predicate evaluates to true.
import polars as pl
df = pl.DataFrame({"age": [17, 21, 30], "active": [True, True, False]})
result = df.filter(
(pl.col("age") >= 18) & pl.col("active")
)
print(result)Keeps rows whose age is at least 18 and whose active flag is true.
Keep records meeting one or more column-based conditions.
Apply compact equality filters with named keyword arguments.
Combine computed expressions to enforce business rules before downstream transformations.
DataFrame.filter do with false or null predicates?It returns a DataFrame containing only rows for which the predicate resolves to true. False and null predicate results are discarded, while the remaining rows keep their original order.
Follow-up: How are multiple conditions combined? Use boolean expression operators such as & and |, or pass multiple conditions; ensure each expression is parenthesized as needed for Python operator precedence.
filter accept?A predicate is an expression that evaluates to a boolean Series. Keyword constraints provide column filters, and each behaves like pl.col(name).eq(value).
Follow-up: How would you filter on a computed condition? Build an expression from column expressions, then pass it to filter rather than materializing a temporary column.
Each keyword constraint is implicitly combined with the other filter conditions using &. For example, df.filter(status="ok", score=10) requires both equality checks to hold.
Follow-up: What happens when a constrained column contains null? Equality can yield null, so the corresponding row is discarded unless null handling is made explicit.
polars.Expr.filterpolars.LazyFrame.filterpolars.DataFrame.remove