Row selection
Select records meeting numeric or categorical conditions before further transformations.
Polars · Function
Filters a DataFrame to rows whose predicate expressions evaluate to true. It preserves their order and supports keyword equality constraints that combine with other conditions.
Call DataFrame.filter with one or more expressions that produce Boolean series, or use keyword constraints for equality checks. Conditions are combined with logical AND, and the returned DataFrame keeps the input order of rows that evaluate to true.
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 or iterable of expressions that evaluate to a Boolean Series; Boolean values and supported Boolean arrays are also accepted. |
**constraints | Any | Keyword column filters. Each name=value condition is equivalent to equality with that column and is combined with other conditions using AND. |
A DataFrame containing only rows retained by the predicate conditions.
import polars as pl
df = pl.DataFrame({"score": [3, 8, 5], "team": ["A", "B", "A"]})
result = df.filter(
pl.col("score") >= 5,
team="A",
)
print(result)Keeps rows whose score is at least 5 and whose team equals A.
Select records meeting numeric or categorical conditions before further transformations.
Apply several required conditions without manually joining every expression with &.
Use concise column=value syntax for straightforward equality checks.
DataFrame.filter return?DataFrame.filter returns a DataFrame containing rows whose predicate expressions evaluate to true. It preserves the original order of the retained rows.
Follow-up: What happens when a predicate produces null? The row is discarded because only true results are retained.
Pass expressions such as pl.col("score") >= 5 as predicates. Multiple predicates can be supplied separately and are combined as conjunctive conditions.
Follow-up: How would you express a disjunction? Combine expressions with the | operator inside a predicate.
A keyword constraint such as team="A" is treated like pl.col("team").eq("A"). Constraints are implicitly combined with other filter conditions using &.
Follow-up: What is the null caveat for equality constraints? A null comparison produces null, so that row is not retained.
polars.DataFrame.removepolars.LazyFrame.filterpolars.Expr.filter