• Post author:
  • Post category:Pandas
  • Post last modified:May 19, 2024
  • Reading time:12 mins read
You are currently viewing Pandas Delete Last Row From DataFrame

To delete the last row from a pandas DataFrame, you can use the drop() method with the index of the last row. In this article, I will explain how to remove the last row of data from DataFrame by using either drop(), iloc[], or head() functions.

Advertisements

Key Points –

  • The drop() method in Pandas can be used to delete the last row from a DataFrame by specifying the index of the row to be removed.
  • Ensure to provide the index of the row you want to delete, typically using df.index[-1] to select the last row.
  • Optionally, set inplace=True if you want to modify the original DataFrame without creating a copy.
  • The iloc[] indexing method allows for row selection in a DataFrame, making it possible to drop the last row by excluding it through slicing.
  • The head() function can be to drop the last row by specifying the desired number of rows to include, effectively excluding the last row.

Quick Examples of Delete Last Row

Following are quick examples of deleting the last row from Pandas DataFrame.


# Quick examples of delete last row

# By using iloc[] 
# To select all rows except the last row
df2 = df.iloc[:-1 , :]

# Using drop() function 
# To delete last row
df.drop(index=df.index[-1],axis=0,inplace=True)

# Using DataFrame.head() function 
# To drop last row
df2 = df.head(df.shape[0] -1)

To run some examples of deleting the last row from Pandas DataFrame, let’s create Pandas DataFrame using data from a dictionary.


# Create DataFrame
import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,24000],
    'Duration':['30day','40days','35days','60days'],
    'Discount':[1000,2300,2500,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r1    Spark  20000    30day      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r4   pandas  24000   60days      2000

Drop Last Row of Pandas DataFrame Using iloc[]

To drop the last row of a Pandas DataFrame using iloc[]. By using DataFrame.iloc[] you can drop the rows from DataFrame and use -1 to drop the last row.


# By using iloc[] 
# To select all rows except the last row
df2 = df.iloc[:-1 , :]
print(df2)

In the above example, use df.iloc[:-1,:] to select all rows except the last one and then assign it back to the original variable which ideally drops the last row from DataFrame. As a result, the last row is effectively dropped from the DataFrame.


# Output:
    Courses    Fee Duration  Discount
r1    Spark  20000    30day      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500

Delete Last Row of Pandas DataFrame Using drop()

Alternatively, you can also use drop() method to remove the last row. Use index param to specify the last index and inplace=True to apply the change on the existing DataFrame. In the below example, df.index[-1] returns r3 which is the last row from our DataFrame.


# Using drop() function to delete last row
df.drop(index=df.index[-1],axis=0,inplace=True)
print(df)

Yields the same output as above.

Drop Last Row of Pandas DataFrame Using head()

The head() function in Pandas returns the first n rows of a DataFrame. If you want to drop the last row using head(), you can use it in combination with the length of the DataFrame to exclude the last row.


# Use head() to drop the last row
df = df.head(len(df) - 1)
print(df)

In the above example, df.head(len(df) - 1) returns all rows except the last one, effectively dropping the last row from the DataFrame.

Similarly, using DataFrame.head() in combination with DataFrame.shape[0] to drop the last row from the DataFrame. The df.shape[0] gives you the number of rows in the DataFrame, and by subtracting 1, you get all rows except the last one.


# Using DataFrame.head() function 
# To drop last row
df2 = df.head(df.shape[0] - 1)
print(df2)

Yields the same output as above.

Frequently Asked Questions on Delete Last Row of Data

How can I verify that the last row has been successfully deleted?

You can print the DataFrame or check its length before and after the deletion to verify that the last row has been removed.

Can I achieve the same result using head() function?

The head() function can be used to exclude the last row by specifying the number of rows to include. You can subtract 1 from the total number of rows in the DataFrame.

Can I revert the deletion of the last row?

The drop method is irreversible unless you have a backup of the DataFrame or are working with a copy. Ensure to review and confirm the deletion before executing the operation.

Are there alternatives to drop for deleting the last row?

ou can use slicing to exclude the last row or iloc to select all rows except the last one. However, the drop method offers more flexibility for general row or column deletion.

Is there a difference between these methods in terms of performance?

The performance difference is likely to be negligible for small to medium-sized DataFrames. Choose the method that aligns with your coding style and preference. For very large DataFrames, performance considerations might become more relevant.

Conclusion

In conclusion, removing the last row from a Pandas DataFrame is essential for data manipulation tasks. Through this article, you’ve gained insights into three different methods for accomplishing this task: DataFrame.iloc[], DataFrame.drop(), and DataFrame.head(). Understanding these methods equips you with the flexibility to choose the most suitable approach based on your specific requirements.

Happy Learning !!

References

Leave a Reply