Home > OS >  How to filter df by column's value NOT in value list with Polars?
How to filter df by column's value NOT in value list with Polars?

Time:01-04

My last question about filter df by value list had a nice solution: How to filter df by value list with Polars?

But now I have inverse task.

I have a list with some int values: black_list = [45, 87, 555] And I have df with some values in column cid1.

df = pl.DataFrame(
    {
        "cid1": [45, 99, 177],
        "cid2": [4, 5, 6],
        "cid3": [7, 8, 9],
    }
)

How I can filter df by my black_list to result df contains only rows without blacklisted values in the "cid1" column?

I can't filter by some white_list according to the conditions of my task.

The code .filter((pl.col("cid1").is_not(black_list)) not suitable. I tried it but it get me an error TypeError: Expr.is_not() takes 1 positional argument but 2 were givenand I don't catch another way.

Thank you!

CodePudding user response:

You can just add ~ to get reversed Series of bool values

df.filter(~col("cid1").is_in(black_list))

or you can use .is_not() to reverse bool values

df.filter(col("cid1").is_in(black_list).is_not())
  • Related