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
<div class="alert alert-info flex not-prose">![]()
<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 6Here 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
This will output:
A B
0 1 4You can also use multiple conditions by & for and and | for or.
Use multiple conditions in filtering a Pandas DataFrame in Python
This will output:
A B C
2 3 6 9