2

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.

1 Answer 1

4

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())
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! All this variants is the solutions for me! Only not just 'col' but I print 'pl.col'
Oh, forgot about it. I always use col - it's a bit shorter)) (from polars import col after importing polars)
Do note that at least from polars v1.33.0 (maybe earlier), using is_in as shown in this solution will throw a warning that its behaviour will be deprecated (see also github.com/pola-rs/polars/pull/22178). Now, it expects a .implode() after the list: df.filter(~col("cid1").is_in(black_list).implode())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.