How to Drop Rows in Pandas : Know Various Approaches

How to Drop Rows in Pandas

Want to drop rows in pandas dataframe? If yes then this post is for you. I have already explained how to drop columns in pandas. In this post, I will show you the various method to delete or drop rows in pandas.

Steps to Drop Rows in Pandas Dataframe

Step 1: Import necessary libraries.

In our example, I am using only two libraries NumPy and pandas and DateTime. So I am importing all of them using the import statement.

import numpy as np
import pandas as pd
import datetime

Step 2: Create a Pandas Dataframe for Executing Examples.

I am creating a time-series dataframe with “Date” as the index column and three columns A, B,C. Use the below lines of code to create that. Please note that I am not explaining all the methods used in the creation of dataframe. Because It can make this post long. Our main objective is to tell you how to drop rows from pandas dataframe.

import numpy as np
import pandas as pd
import datetime

todays_date = datetime.datetime.now().date()
index = pd.date_range(todays_date-datetime.timedelta(10), periods=10, freq='D')

columns = ['A','B', 'C']
data = np.array([np.arange(10)]*3).T
df = pd.DataFrame(data,index=index, columns=columns)

Output

Sample Dataframe Creation for dropping rows
Sample Dataframe Creation for dropping rows

Step 3: Use the various approaches to Drop rows

Approach 1: How to Drop First Row in pandas dataframe

To remove the first row you have to pass df. index[[0]]  inside the df.drop() method. It will successfully remove the first row.

df.drop(df.index[[0]])

Now you will get all the dataframe values except the “2020-11-14” row.

Output

Output of dataframe after removing the first row
Output of dataframe after removing the first row

 

Approach 2: How to drop certain rows in pandas

In approach 1, you have to drop the first row by adding 0 to the list. Now Suppose I have to drop row 3,5,8 then I will make it a list and pass it to the df.dropna() method. Please note that rows are counted from 0 onwards.

df.drop(df.index[[2,4,7]])

Output

Output of dataframe after removing the 3,5,and 8 Rows
Output of dataframe after removing the 3,5, and 8 Rows

Approach 3: How to drop a row based on conditions in pandas

Sometimes you have to remove rows from dataframe based on some specific condition. It can be done by passing the condition df[your_conditon] inside the drop() method.

For example, I want to drop rows that have a value greater than 4 of Column A. Then I will use df[df[“A]>4] as a condition. Execute the following lines of code.

df.drop(df[df["A"]>4].index)

Output

Output of dataframe after removing the rows that have value greater than 4 in Column A
The output of dataframe after removing the rows that have a value greater than 4 in Column A

 

In the same way, you can do it for other columns also.

Approach 4: Drop a row by index name in pandas

Suppose you have dataframe with the index name in it. And You want to drop a row by index name then you can do so. In this section, I will create another dataframe with the index name or labels. Then I will delete the row based on the index name.

person = {"name":["Sahil","John","Abhishek","Robin"],"age":[20,25,35,37]}
person_df = pd.DataFrame(person,index=["p1","p2","p3","p4"])

Output

Output of the Person Dataframe
The output of the Person Dataframe

Now I want to drop the person p3, then I will pass “p3” inside the drop()  method.

person_df.drop("p3")

You will get the following output.

Dataframe after removing the p3 person
Dataframe after removing the p3 person

In the same way, you can pass the index name as a list to remove multiple rows.

person_df.drop(["p3","p4"])

Output

Dataframe after removing the p3 and p4 person
Dataframe after removing the p3 and p4 person

 

That’s all for now. These are steps and approaches to drop rows in pandas. When you have dataframe with an index column then dropping rows becomes an easy task. And even if your dataframe has no index column then you can make it using df.set_index(“your_column_name).

drop rows pandas
drop rows pandas

Hope this article has cleared your understanding of how to drop rows in pandas. Even if you have any queries then you can contact us for more information.

Source:

Pandas drop() Method

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner