How do I use isin for multiple columns?

The isin() method can be used to check for the existence of multiple columns in a DataFrame. This method takes a list of values as its argument and returns True if the value is present in any column, and False if not. This method is useful for filtering out rows with certain criteria and can be used to quickly identify which rows contain the desired values.


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.

x