polars.Expr.and_#

Expr.and_(*others: Any) Expr[source]#

Method equivalent of bitwise “and” operator expr & other & ....

This has the effect of combining logical boolean expressions, but operates bitwise on integers.

Parameters:
*others

One or more integer or boolean expressions to evaluate/combine.

Examples

>>> df = pl.DataFrame(
...     data={
...         "x": [5, 6, 7, 4, 8],
...         "y": [1.5, 2.5, 1.0, 4.0, -5.75],
...         "z": [-9, 2, -1, 4, 8],
...     }
... )

Combine logical “and” conditions:

>>> df.select(
...     (pl.col("x") >= pl.col("z"))
...     .and_(
...         pl.col("y") >= pl.col("z"),
...         pl.col("y") == pl.col("y"),
...         pl.col("z") <= pl.col("x"),
...         pl.col("y") != pl.col("x"),
...     )
...     .alias("all")
... )
shape: (5, 1)
┌───────┐
│ all   │
│ ---   │
│ bool  │
╞═══════╡
│ true  │
│ true  │
│ true  │
│ false │
│ false │
└───────┘

Bitwise “and” operation on integer columns:

>>> df.select("x", "z", x_and_z=pl.col("x").and_(pl.col("z")))
shape: (5, 3)
┌─────┬─────┬─────────┐
│ x   ┆ z   ┆ x_and_z │
│ --- ┆ --- ┆ ---     │
│ i64 ┆ i64 ┆ i64     │
╞═════╪═════╪═════════╡
│ 5   ┆ -9  ┆ 5       │
│ 6   ┆ 2   ┆ 2       │
│ 7   ┆ -1  ┆ 7       │
│ 4   ┆ 4   ┆ 4       │
│ 8   ┆ 8   ┆ 8       │
└─────┴─────┴─────────┘