Often you may want to use the isin() function within the query() method in pandas to filter for rows in a DataFrame where a column contains a value in a list.
You can use the following syntax to do so:
df.query('team in ["A", "B", "D"]')
This particular query filters for rows in a pandas DataFrame where the team column is equal to A, B, or D.
Note: We must use in instead of isin when using the pandas query() method.
The following example shows how to use this syntax in practice.
Example: Use query() Method to Filter for Values in List
Suppose we have the following pandas DataFrame that contains information about various basketball players:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'E'], 'points': [18, 22, 19, 14, 14, 11, 20, 28], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame print(df) team points assists rebounds 0 A 18 5 11 1 A 22 7 8 2 B 19 7 10 3 B 14 9 6 4 C 14 12 6 5 C 11 9 5 6 D 20 9 9 7 E 28 4 12
Now suppose that we would like to query for the rows where the value in the team column is equal to either A, B, or D.
We can use the following syntax to do so:
#query for rows where team is in list of specific teams df.query('team in ["A", "B", "D"]') team points assists rebounds 0 A 18 5 11 1 A 22 7 8 2 B 19 7 10 3 B 14 9 6 6 D 20 9 9
Notice that the query() function returns all rows where the value in the team column is equal to A, B, or D.
Also note that we can store a list of team names in a variable and then reference the variable in the query() function using the @ operator:
#create variable to hold specific team names team_names = ["A", "B", "D"] #query for rows where team is equal to a team name in team_names variable df.query('team in @team_names') team points assists rebounds 0 A 18 5 11 1 A 22 7 8 2 B 19 7 10 3 B 14 9 6 6 D 20 9 92
The query returns all of the rows in the DataFrame where team is equal to one of the team names stored in the team_names variable.
Notice that the results of this query match the one from the previous example.
Note: You can find the complete documentation for the pandas query() function here.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Pandas: How to Filter Rows Based on String Length
Pandas: How to Drop Rows Based on Condition
Pandas: How to Use “NOT IN” Filter