In this post, we are going to learn different ways of how Pandas select rows by multiple conditions in Pandas by using dataframe loc[] and by using the column value, loc[] with and operator.
1. loc[] to Select mutiple rows based on column value
To select the row from the pandas dataframe we are using the Datafrmae loc[]. The loc[] access the group of rows and columns by the label.
Syntax
df.loc[df['column name'] condition]
In this example, we have to select a subset of dataframe rows for column ‘Name’ where condition name== ‘Rack’.It will select all the matching rows single or multiple and return a subset of the dataframe.
Program Example
#python 3 program to select subset of dataframe in Pandas
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David','Rack','max','Rack'],
'Marks':[100,100,100,100,100,100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic','Physics','Music','math']
}
dfobj = pd.DataFrame(Student_dict,columns=['Name','Marks','Subject'])
#appyling condition selecting subset of dataframe
selected_students = dfobj.loc[dfobj['Name'] == 'Rack']
print (selected_students)
Output
Name Marks Subject
1 Rack 100 Math
4 Rack 100 Physics
6 Rack 100 math
2. Select Mutiple row using loc[] and & operator
To select the rows based on mutiple condition we can use the & operator.In this example we have passed mutiple conditon using this code dfobj.loc[(dobj[‘Name’] == ‘Rack’) & (dobj[‘Marks’] == 100)].
This code will return a subset of dataframe rows where name=’Rack’ and marks =100.
Program example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David','Rack','max','Rack'],
'Marks':[100,100,100,100,100,100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic','Physics','Music','math']
}
dfobj = pd.DataFrame(Student_dict,columns=['Name','Marks','Subject'])
selected_students = dfobj.loc[(dobj['Name'] == 'Rack') & (dobj['Marks'] == 100)]
print (selected_students)
output
Name Marks Subject
1 Rack 100 Math
4 Rack 100 Physics
6 Rack 100 math
3. Select row based on Value in Column
We can simply use dataframe column name to select the rows based on column value ,we have used column ‘Name‘ and ,passed condition where name == ‘Rack’ .In this line of code dfobj[(dfobj [‘Name’] == ‘Rack’)].It select all the matching rows single or mutiple.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David','Rack','max','Rack'],
'Marks':[100,100,100,100,100,100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic','Physics','Music','math']
}
dfobj = pd.DataFrame(Student_dict,columns=['Name','Marks','Subject'])
selected_students = dfobj[(dfobj ['Name'] == 'Rack')]
print (selected_students)
Output
Name Marks Subject
1 Rack 100 Math
4 Rack 100 Physics
6 Rack 100 math
4. Select row based on multiple values in column
To select the rows based on mutiple column value ,we have used column ‘marks‘ and passed mutiple condition using and operator (&) . It will select the rows based on mutiple condition.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David','Rack','max','Rack'],
'Marks':[100,90,92,100,100,100,99],
'Subject': ['Math', 'Math', 'Music', 'Physic','Physics','Music','math']
}
dfobj = pd.DataFrame(Student_dict,columns=['Name','Marks','Subject'])
subset_of_df = dfobj[(dfobj['Marks'] >=90) & (dfobj['Marks'] < 100) ]
print (subset_of_df)
Output
Name Marks Subject
1 Rack 90 Math
2 Max 92 Music
6 Rack 99 math
5. Select row based on any of mutiple condition
Sometimes instead of selecting on mutiple conditions, we have to select all or any matching rows of a given column that we can achieve by using the isin() method and can pass mutiple values.
In this example we have passed mutiple conditions using .isin([‘Math’, ‘Physic’]) ] it will select all rows or any row have subject ‘Math’ or ‘Physic’ and
Program Example
#python 3 program to selectdataframe rows by condition
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David','Rack','max','Rack'],
'Marks':[100,90,92,100,100,100,99],
'Subject': ['Math', 'Math', 'Music', 'Physic','Physics','Music','math']
}
dfobj = pd.DataFrame(Student_dict,columns=['Name','Marks','Subject'])
subset_of_df = dfobj[dfobj['Subject'].isin(['Math', 'Physic']) ]
print (subset_of_df)
Output
Name Marks Subject
0 Jack 100 Math
1 Rack 90 Math
3 David 100 Physic
6. Select Mutiple Rows using OR operator
In this example, we are using the ‘and’ operator to select any or both matching columns value passed in condition.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David','Rack','max','Rack'],
'Marks':[100,90,92,100,100,100,99],
'Subject': ['Math', 'Math', 'Music', 'Physic','Physics','Music','Math']
}
dfobj = pd.DataFrame(Student_dict,columns=['Name','Marks','Subject'])
subset_of_df = dfobj[(dfobj['Name'] == 'Jack') | (dfobj['Subject'] == 'Math')]
print (subset_of_df)
Output
Name Marks Subject
0 Jack 100 Math
1 Rack 90 Math
6 Rack 99 Math
Summary
In this post, we have learned multiple ways to Select rows by multiple conditions in Pandas with code example by using dataframe loc[] and by using the column value, loc[] with & and or operator. We can change operators as per need like <=,>=,!=.