Drop Last Row in Pandas : Steps and Methods

Drop Last Row in Pandas

Pandas is a python package that allows you to create datasets into dataframe. After that, you can manipulate them easily using various inbuilt functions. Suppose you have adataframe and want to delete the last row then how you can do so? In this tutorial, you will know how to drop the last row of a dataframe in pandas through steps.

Steps to drop the last row in pandas

In this section, you will know all the steps required to drop the last row of pandas dataframe. You should follow all these steps for better understanding.

Step 1: Import required libraries

The first step is to import all the required libraries that will be used in our example. I am using the panda’s package only. So let’s import them using the import statement.

import pandas as pd

Step 2: Create sample dataframe

The next step is to create a sample dataframe that allows you to implement the removal of the last row of pandas. Use the below lines of code to create the sample dataframe.

import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30], 'B': [40, 50, 60], 'C': [70, 80, 90]})
print(df)

Output

sample dataframe for implementing remobal of the last row of the dataframe
sample dataframe for implementing removal of the last row of the dataframe

Step 3: Implement the following methods

After the creation of datframe use, the following method to drop the last row of the pandas dataframe.

Method 1: Drop the last row in pandas using the drop() function

In this method, you will use pandas.drop() function to remove the last row of the dataframe. You have to just pass the df.index[-1] as an argument for the drop() function. Use the below lines of code to remove the last row of the dataframe.

import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30], 'B': [40, 50, 60], 'C': [70, 80, 90]})
df = df.drop(df.index[-1])
print(df)

Output

removing last row of dataframe using drop() function
removing last row of dataframe using drop() function

Method 2: Using the iloc[] attribute

You can also use the iloc[] attribute to remove the last row of the datafframe. You will use the df.iloc[-1] indexer. It will select all the rows of the dataframe except the last one.

Use the below lines of code to implement it.

import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30], 'B': [40, 50, 60], 'C': [70, 80, 90]})
df =df.iloc[:-1]
print(df)

Output

removing last row of dataframe using iloc indexer
removing last row of dataframe using iloc indexer

Conclusion

These are the steps to remove the last row of the pandas datframe. In this tutorial, you have learned two methods to remove the row. One is the drop() function and the other is iloc[] indexer. You can use any one of them as per requirement.

I hope you have liked this tutorial. If you have any queries or doubts then you can contact us for more help.

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