Table of Contents
Filtering a Pandas DataFrame based on multiple conditions is a common task in data analysis and manipulation. To achieve this, one can use the “loc” method in Pandas, which allows for selecting rows and columns based on a certain criteria. The “loc” method can be used to specify multiple conditions using logical operators such as “and” and “or”. By using this method, one can efficiently filter a DataFrame based on multiple criteria and obtain the desired subset of data for further analysis.
Filter a Pandas DataFrame on Multiple Conditions
Often you may want to filter a pandas DataFrame on more than one condition. Fortunately this is easy to do using boolean operations.
This tutorial provides several examples of how to filter the following pandas DataFrame on multiple conditions:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'C'], 'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'rebounds': [11, 8, 10, 6, 6]}) #view DataFrame df team points assists rebounds 0 A 25 5 11 1 A 12 7 8 2 B 15 7 10 3 B 14 9 6 4 C 19 12 6
Example 1: Filter on Multiple Conditions Using ‘And’
The following code illustrates how to filter the DataFrame using the and (&) operator:
#return only rows where points is greater than 13 and assists is greater than 7 df[(df.points > 13) & (df.assists > 7)] team points assists rebounds 3 B 14 9 6 4 C 19 12 6 #return only rows where team is 'A' and points is greater than or equal to 15 df[(df.team == 'A') & (df.points >= 15)] team points assists rebounds 0 A 25 5 11
Example 2: Filter on Multiple Conditions Using ‘Or’
The following code illustrates how to filter the DataFrame using the or (|) operator:
#return only rows where points is greater than 13 or assists is greater than 7 df[(df.points > 13) | (df.assists > 7)] team points assists rebounds 0 A 25 5 11 2 B 15 7 10 3 B 14 9 6 4 C 19 12 6 #return only rows where team is 'A' or points is greater than or equal to 15 df[(df.team == 'A') | (df.points >= 15)] team points assists rebounds 0 A 25 5 11 1 A 12 7 8 2 B 15 7 10 4 C 19 12 6
Example 3: Filter on Multiple Conditions Using a List
The following code illustrates how to filter the DataFrame where the row values are in some list.
#define a list of values filter_list = [12, 14, 15] #return only rows where points is in the list of values df[df.points.isin(filter_list)] team points assists rebounds 1 A 12 7 8 2 B 15 7 10 3 B 14 9 6 #define another list of values filter_list2 = ['A', 'C'] #return only rows where team is in the list of values df[df.team.isin(filter_list2)] team points assists rebounds 0 A 25 5 11 1 A 12 7 8 4 C 19 12 6
You can find more pandas tutorials here.