ssert_frame_equal.html)
    * [ polars.testing.assert_frame_not_equal ](../../api/polars.testing.assert_frame_not_equal.html)
    * [ polars.testing.assert_series_equal ](../../api/polars.testing.assert_series_equal.html)
    * [ polars.testing.assert_series_not_equal ](../../api/polars.testing.assert_series_not_equal.html)
    * [ polars.testing.parametric.dataframes ](../../api/polars.testing.parametric.dataframes.html)
    * [ polars.testing.parametric.series ](../../api/polars.testing.parametric.series.html)
    * [ polars.testing.parametric.column ](../../api/polars.testing.parametric.column.html)
    * [ polars.testing.parametric.columns ](../../api/polars.testing.parametric.columns.html)
    * [ polars.testing.parametric.create_list_strategy ](../../api/polars.testing.parametric.create_list_strategy.html)
    * [ polars.testing.parametric.load_profile ](../../api/polars.testing.parametric.load_profile.html)
    * [ polars.testing.parametric.set_profile ](../../api/polars.testing.parametric.set_profile.html)

  * [ Metadata ](../../metadata.html) __
    * [ polars.build_info ](../../api/polars.build_info.html)
    * [ polars.get_index_type ](../../api/polars.get_index_type.html)
    * [ polars.show_versions ](../../api/polars.show_versions.html)
    * [ polars.thread_pool_size ](../../api/polars.thread_pool_size.html)
    * [ polars.threadpool_size ](../../api/polars.threadpool_size.html)

  * [ __ ](../../../index.html)
  * [ Python API reference ](../../index.html)
  * __
  * [ Manipulation/selection ](../modify_select.html)
  * polars.Expr.filter 

#  polars.Expr.filter  #

Expr.  filter  (

     _ *  predicates  :  IntoExprColumn  |  Iterable  [  IntoExprColumn  ]  _ , 
     _ **  constraints  :  Any  _ , 
)  →  Self  [ [source]  ](https://github.com/pola-
rs/polars/blob/py-0.20.23/py-polars/polars/expr/expr.py#L3996-L4077) #

    

Filter the expression based on one or more predicate expressions.

The original order of the remaining elements is preserved.

Mostly useful in an aggregation context. If you want to filter on a DataFrame
level, use ` LazyFrame.filter  ` .

Parameters  :

    

**predicates**

    

Expression(s) that evaluates to a boolean Series.

**constraints**

    

Column filters; use ` name  =  value  ` to filter columns by the supplied
value. Each constraint will behave the same as ` pl.col(name).eq(value)  ` ,
and will be implicitly joined with the other filter conditions using ` & ` .

Examples

    
    
    >>> df = pl.DataFrame(
    ...     {
    ...         "group_col": ["g1", "g1", "g2"],
    ...         "b": [1, 2, 3],
    ...     }
    ... )
    >>> df.group_by("group_col").agg(
    ...     lt=pl.col("b").filter(pl.col("b") < 2).sum(),
    ...     gte=pl.col("b").filter(pl.col("b") >= 2).sum(),
    ... ).sort("group_col")
    shape: (2, 3)
    ┌───────────┬─────┬─────┐
    │ group_col ┆ lt  ┆ gte │
    │ ---       ┆ --- ┆ --- │
    │ str       ┆ i64 ┆ i64 │
    ╞═══════════╪═════╪═════╡
    │ g1        ┆ 1   ┆ 2   │
    │ g2        ┆ 0   ┆ 3   │
    └───────────┴─────┴─────┘
    

Filter expressions can also take constraints as keyword arguments.

    
    
    >>> import polars.selectors as cs
    >>> df = pl.DataFrame(
    ...     {
    ...         "key": ["a", "a", "a", "a", "b", "b", "b", "b", "b"],
    ...         "n": [1, 2, 2, 3, 1, 3, 3, 2, 3],
    ...     },
    ... )
    >>> df.group_by("key").agg(
    ...     n_1=pl.col("n").filter(n=1).sum(),
    ...     n_2=pl.col("n").filter(n=2).sum(),
    ...     n_3=pl.col("n").filter(n=3).sum(),
    ... ).sort(by="key")
    shape: (2, 4)
    ┌─────┬─────┬─────┬─────┐
    │ key ┆ n_1 ┆ n_2 ┆ n_3 │
    │ --- ┆ --- ┆ --- ┆ --- │
    │ str ┆ i64 ┆ i64 ┆ i64 │
    ╞═════╪═════╪═════╪═════╡
    │ a   ┆ 1   ┆ 4   ┆ 3   │
    │ b   ┆ 1   ┆ 2   ┆ 9   │
    └─────┴─────┴─────┴─────┘
    

[ __ previous  polars.Expr.fill_null  ](polars.Expr.fill_null.html "previous
page") [ next  polars.Expr.flatten  __ ](polars.Expr.flatten.html "next page")

__