In this post, we are going to understand how to Drop rows by multiple conditions in Pandas Dataframe with the help of dataframe inbuilt function drop(). We will drop rows by single or multiple conditions.
1. Drop rows by condition in Pandas dataframe
The Pandas dataframe drop() method takes single or list label names and delete corresponding rows and columns.The axis = 0 is for rows and axis =1 is for columns.
In this example, we are deleting the row that ‘mark’ column has value =100 so three rows are satisfying the condition. The single row will be displayed in Output.
Program Example
#python 3.x
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,100, 99,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
#filtering index where marks =100
indexes = df[df['Marks'] == 100].index
#droping row based on column value
df.drop(indexes,inplace=True)
print(df)
Output
Name Marks Subject
R3 Max 99 Music
2. Drop rows by mutiple condition on same column value
In this example, we are dropping rows by applying multiple conditions on the same ‘mark’ column where marks =100 and marks >98. So there are two rows which are satisfying the condition. So it displays two rows in the resulting output.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,98, 99,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
#filtering index where marks =100
indexes = df[ (df['Marks'] == 100) & (df['Marks']>98) ].index
#droping mutiple rows based on column value
df.drop(indexes,inplace=True)
print(df)
Output
Name Marks Subject
R2 Rack 98 Math
R3 Max 99 Music
3. Drop rows by mutiple condition different column values
In this example, we are dropping rows by applying multiple conditions on different columns ‘mark’ and ‘Subject’. where marks ==100 and Subject ==Math .So there are two rows which are satisfying the condition. So it displays two rows in the resulting output.
Program Exmaple
#python 3.x
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,100, 99,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
#filtering index where marks =100
indexes = df[ (df['Marks'] == 100) & (df['Subject'] == 'Math') ].index
#droping mutiple rows based on column value
df.drop(indexes,inplace=True)
print(df)
Output
Name Marks Subject
R3 Max 99 Music
R4 David 100 Physic
4. Boolean masking to drop rows by column values
The simplest way to drop rows from the dataframe by condition on columns values is Boolean masking as we are achieving in this below example.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,98, 99,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
#filtering rows based on column values
filter_rows = df[(df.Marks > 98) & (df.Subject== 'Math')]
print(filter_rows)
Output
Name Marks Subject
R1 Jack 100 Math
5. Drop mutiple columns from dataframe
We can drop single or multiple columns from the dataframe just by passing the name of columns and by setting up the axis =1. As we are doing in the below example.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,98, 99,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
#droping mutiple coulns from dataframe
result_df = df.drop(['Name','Marks'], axis = 1)
print(result_df)
Output
Subject
R1 Math
R2 Math
R3 Music
R4 Physic
Summary
In this post, we have learned how to Drop rows by multiple conditions in Pandas Dataframe with code example by using the drop() method of dataframe. We have dropped the single or multiple rows by applying multiple conditions in the dataframe.