In this post, we are going to learn how to Drop one or multiple rows in Pandas Dataframe by using the dataframe built-in method drop(). It is used to drop a row or multiple rows from the dataframe by index label or using the column name.
Pandas Dataframe.drop() method
The Dataframe.drop() method drop the specified label row or columns. It removes specified label names and corresponding axis or directly specifying index or column names.
Syntax
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
Parameters
- label: single or list label that specifies row or column label to drop.
- axis: It cab be int or string value, 0 ‘index’ for Rows and 1 ‘columns’ for Columns.
- Index /columns: Alternative to axis.
- level: index or level name,For MultiIndex, level from which the labels will be removed.
- inplace : if true amkes chnages in original dataframe else return a copy.
- errors: It has two values ‘ignore’ or ‘raise’ default is ‘raise’.
1. Drop single row in DataFrame
In this example, we are dropping a row in the dataframe by passing the index label to the drop() method and after deleting it is returning the result dataframe.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,100, 100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
result_df = df.drop('R1')
print(result_df)
Output
Name Marks Subject
R2 Rack 100 Math
R3 Max 100 Music
R4 David 100 Physic
2. Drop mutiple rows in dataframe
In this example, we are passing the multiple index label to drop() method to drop multiple rows from the dataframe.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,100, 100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
result_df = df.drop(['R2','R4'])
print(result_df)
Output
Name Marks Subject
R1 Jack 100 Math
R3 Max 100 Music
3. Drop list of rows by index label in Dataframe
We can pass multiple row index labels as a list to drop() method of dataframe to drop a list of rows from the dataframe. We have passed the inplace=’True’ argument because we do not want to return a new dataframe changing in the original dataframe.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,100, 100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
df.drop(['R2','R4','R1'],inplace=True)
print(df)
Output
Name Marks Subject
R3 Max 100 Music
4. Drop mutiple rows by index range in dataframe
To drop a range of rows from the dataframe we can pass a range of index to drop() method and it will drop rows in a given index range as we are doing in the below example.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,100, 100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
df.drop(df.index[1:3],inplace=True)
print(df)
Output
Name Marks Subject
R1 Jack 100 Math
R4 David 100 Physic
Summary
In this post, we have learned to Drop one or multiple rows in Pandas Dataframe with code example by using the dataframe inbuilt drop() method by using the above mention way single or multiple rows can be drop at once.