Often you may want to select the rows of a pandas DataFrame in which a certain value appears in any of the columns.
Fortunately this is easy to do using the .any pandas function. This tutorial explains several examples of how to use this function in practice.
Example 1: Find Value in Any Column
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'rebounds': [11, 8, 10, 6, 6]}) #view DataFrame print(df) points assists rebounds 0 25 5 11 1 12 7 8 2 15 7 10 3 14 9 6 4 19 12 6
The following syntax shows how to select all rows of the DataFrame that contain the value 25 in any of the columns:
df[df.isin([25]).any(axis=1)] points assists rebounds 0 25 5 11
The following syntax shows how to select all rows of the DataFrame that contain the values 25, 9, or 6 in any of the columns:
df[df.isin([25, 9, 6]).any(axis=1)] points assists rebounds 0 25 5 11 3 14 9 6 4 19 12 6
Example 2: Find Character in Any Column
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'position': ['G', 'G', 'F', 'F', 'C']}) #view DataFrame print(df) points assists position 0 25 5 G 1 12 7 G 2 15 7 F 3 14 9 F 4 19 12 C
The following syntax shows how to select all rows of the DataFrame that contain the character G in any of the columns:
df[df.isin(['G']).any(axis=1)] points assists position 0 25 5 G 1 12 7 G
The following syntax shows how to select all rows of the DataFrame that contain the values G or C in any of the columns:
df[df.isin(['G', 'C']).any(axis=1)] points assists position 0 25 5 G 1 12 7 G 4 19 12 C
Additional Resources
How to Filter a Pandas DataFrame on Multiple Conditions
How to Find Unique Values in Multiple Columns in Pandas
How to Get Row Numbers in a Pandas DataFrame
What if i have an Age variabile and i want to filter for a specific age group, like from 35 to 50. How would the syntax look. (I want to make a combined filtering, extract only females from gender variable, college grad from education variable, and a specific age group from an age variable). Each record has it’s own age (from 18 to 80)