Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading