Pandas Delete Last Row From DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

You can delete the last row from the pandas DataFrame using either drop(), iloc[] and head() methods. In this article, I will explain how to delete/drop the last row of data from Pandas DataFrame with examples.

  • drop() method is used to remove columns or rows from DataFrame;
  • Use axis param to specify what axis you would like to remove. By default axis = 0 meaning to remove rows. Use axis=1 or columns param to remove columns.
  • Use inplace=True to remove row/column in place meaning on existing DataFrame with out creating copy.

1. Quick Examples of Delete Last Row From Pandas DataFrame

If you are in a hurry, below are some quick examples of how to drop/delete the last row from pandas DataFrame.


# Below are the quick examples

# 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)

Now, let’s create a DataFrame with a few rows and columns and execute some examples and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


# 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

2. Drop Last Row of Pandas DataFrame Using iloc[]

By using DataFrame.iloc[] you can drop the rows from DataFrame and use -1 to drop the last row. For 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.


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

Yields below output.


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

3. Using drop() Function to Delete Last Row of Pandas DataFrame

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 same output as above.

4. Drop Last Row of Pandas DataFrame Using head() Function

You can also use df.head(df.shape[0] -1) to remove the last row of pandas DataFrame.


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

Yields same output as above.

5. Complete Example For Delete Last Row of Data of 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)

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

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

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

Conclusion

In this article, you have learned how to drop the last row data of Pandas DataFrame using DataFrame.iloc[], DataFrame.drop(), and DataFrame.head() function with examples.

Happy Learning !!

References

Naveen

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing Pandas Delete Last Row From DataFrame