W3docs

How to filter Pandas dataframe using 'in' and 'not in' like in SQL

You can filter a Pandas DataFrame using the isin() and ~(not in) methods.

You can filter a Pandas DataFrame using the isin() and ~(not in) methods.

Here is an example of how to filter a DataFrame for rows where a column has a value that is in a list:

Filter a DataFrame for rows where a column has a value that is in a list in Python

python— editable, runs on the server

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

This will output:


A  B
1  2  5
2  3  6

Here is an example of how to filter a DataFrame for rows where a column has a value that is not in a list:

Filter a DataFrame for rows where a column has a value that is not in a list in Python

python— editable, runs on the server

This will output:


A  B
0  1  4

You can also use multiple conditions by & for and and | for or.

Use multiple conditions in filtering a Pandas DataFrame in Python

python— editable, runs on the server

This will output:


A  B  C
2  3  6  9