Pandas: How to Use isin for Multiple Columns


You can use the following methods with the pandas isin() function to filter based on multiple columns in a pandas DataFrame:

Method 1: Filter where Multiple Columns Are Equal to Specific Values

df = df[df[['team', 'position']].isin(['A', 'Guard']).all(axis=1)]

This particular example filters the DataFrame for rows where the team column is equal to ‘A’ and the position column is equal to ‘Guard.’

Method 2: Filter where At Least One Column is Equal to Specific Value

df = df[df[['team', 'position']].isin(['A', 'Guard']).any(axis=1)] 

This particular example filters the DataFrame for rows where the team column is equal to ‘A’ or the position column is equal to ‘Guard.’

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['Guard', 'Guard', 'Forward', 'Forward',
                                'Guard', 'Guard', 'Forward', 'Forward'],
                   'points': [11, 18, 10, 22, 26, 35, 19, 12]})
                   
#view DataFrame
print(df)

  team position  points
0    A    Guard      11
1    A    Guard      18
2    A  Forward      10
3    A  Forward      22
4    B    Guard      26
5    B    Guard      35
6    B  Forward      19
7    B  Forward      12

Example 1: Filter where Multiple Columns Are Equal to Specific Values

We can use the following syntax to filter the DataFrame to only contain rows where the team column is equal to ‘A’ and the position column is equal to ‘Guard.’

#filter rows where team column is 'A' and position column is 'Guard'
df = df[df[['team', 'position']].isin(['A', 'Guard']).all(axis=1)]

#view filtered DataFrame
print(df)

  team position  points
0    A    Guard      11
1    A    Guard      18

Notice that only the rows where the team column is equal to ‘A’ and the position column is equal to ‘Guard’ remain in the filtered DataFrame.

Example 2: Filter where At Least One Column is Equal to Specific Value

We can use the following syntax to filter the DataFrame to only contain rows where the team column is equal to ‘A’ or the position column is equal to ‘Guard.’

#filter rows where team column is 'A' or position column is 'Guard'
df = df[df[['team', 'position']].isin(['A', 'Guard']).any(axis=1)]

#view filtered DataFrame
print(df)

  team position  points
0    A    Guard      11
1    A    Guard      18
2    A  Forward      10
3    A  Forward      22
4    B    Guard      26
5    B    Guard      35

Notice that only the rows where the team column is equal to ‘A’ or the position column is equal to ‘Guard’ remain in the filtered DataFrame.

Note: You can find the complete documentation for the pandas isin() function here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

Pandas: How to Add Filter to Pivot Table
Pandas: How to Filter for “Not Contains”
Pandas: How to Filter Rows that Contain a Specific String

Leave a Reply

Your email address will not be published. Required fields are marked *