polars.Expr.arr.contains#

Expr.arr.contains(item: IntoExpr, *, nulls_equal: bool = True) → Expr[source]#

Check if sub-arrays contain the given item.

engine:In MemoryStreamingDistributed
Parameters:
item

Item that will be checked for membership

nulls_equalbool, default True

If True, treat null as a distinct value. Null values will not propagate. Note that Expr.is_in() defaults to False.

Returns:
Expr

Expression of data type Boolean.

Notes

For supported dtype pairs, lossy conversions do not create matches. Values that would overflow or require rounding match nothing. Cast explicitly to compare integers and floats.

Examples

>>> df = pl.DataFrame(
...     {"a": [["a", "b"], ["x", "y"], ["a", "c"]]},
...     schema={"a": pl.Array(pl.String, 2)},
... )
>>> df.with_columns(contains=pl.col("a").arr.contains("a"))
shape: (3, 2)
┌───────────────┬──────────┐
│ a             ┆ contains │
│ ---           ┆ ---      │
│ array[str, 2] ┆ bool     │
╞═══════════════╪══════════╡
│ ["a", "b"]    ┆ true     │
│ ["x", "y"]    ┆ false    │
│ ["a", "c"]    ┆ true     │
└───────────────┴──────────┘