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.
Here is an example of how to filter a DataFrame for rows where a column has a value that is in a list:
import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# create a list of values to filter for
values_to_filter = [2, 3]
# use the isin() method to filter the DataFrame
filtered_df = df[df['A'].isin(values_to_filter)]
print(filtered_df)
Watch a video course
Python - The Practical Guide
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:
import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# create a list of values to filter for
values_to_filter = [2, 3]
# use the ~ (not in) operator along with the isin() method to filter the DataFrame
filtered_df = df[~df['A'].isin(values_to_filter)]
print(filtered_df)
This will output:
A B 0 1 4
You can also use multiple conditions by &
for and and |
for or.
# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]})
# create a list of values to filter for
values_to_filter = [2, 3]
# use the ~ (not in) operator along with the isin() method to filter the DataFrame
filtered_df = df[(df['A'].isin(values_to_filter)) & (df['C']>8)]
print(filtered_df)
This will output:
A B C 2 3 6 9