Appearance
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:
Filter a DataFrame for rows where a column has a value that is in a list in Python
python
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)
<div class="alert alert-info flex not-prose">Watch a video course Python - The Practical Guide
</div>
This will output:
console
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
python
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:
console
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
python
# 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:
console
A B C
2 3 6 9