Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Module

df.filter

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.

How it works

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.

Syntax

DataFrame.filter(predicates: IntoExprColumn | Iterable[IntoExprColumn] | bool | list[bool] | np.ndarray[Any, Any], **constraints: Any) -> DataFrame

Parameters

NameTypeDescription
predicatesIntoExprColumn | Iterable[IntoExprColumn] | bool | list[bool] | np.ndarray[Any, Any]Expression(s) that evaluate to a boolean Series.
constraintsAnyColumn filters written as name=value; each is an equality condition and constraints are combined with logical AND.

Returns

DataFrame containing only rows whose filter predicate evaluates to true.

Key points

  • The method selects rows according to predicate expressions.
  • Filtering does not reorder the rows that survive.
  • A row must resolve to true to remain in the result.
  • Predicates should produce boolean values aligned with DataFrame rows.
  • Keyword arguments offer concise equality-based column filters.
  • A keyword constraint is equivalent to comparing the named column with the supplied value.
  • Multiple constraints require all supplied conditions to match.

Common mistakes

  • Rows producing null are removed just like rows producing false.
  • Comparisons involving null values do not produce true or false; handle nulls explicitly when their rows must be retained.

Example: Filter rows with expressions

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.

When to use it

Row selection

Keep records meeting one or more column-based conditions.

Equality constraints

Apply compact equality filters with named keyword arguments.

Rule-based preprocessing

Combine computed expressions to enforce business rules before downstream transformations.

Interview questions

QWhat does 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.

QWhat kinds of inputs can 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.

QHow do keyword constraints work?

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.filter
  • polars.LazyFrame.filter
  • polars.DataFrame.remove

Source: polars.DataFrame.filter — Polars documentation. Checked 2026-10-15.