Drop last row of pandas dataframe in python (3 ways)

Dropping last row of pandas dataframe  (3 ways) in python

In this article, we will three different ways to drop/ delete of a pandas dataframe.

Use iloc to drop last row of pandas dataframe :

 Dataframe from Pandas provide an attribute iloc that selects a portion of dataframe. We can take the help of this attribute, as this attribute can select all rows except last one and then assigning these rows back to the original variable, as a result last row will be deleted.

Syntax of dataframe.iloc[]:- df.iloc[row_start:row_end , col_start, col_end]

where,

Arguments:

  • row_start: The row index from which selection is started. Default value is 0.
  • row_end: The row index at which selection should be end i.e. select till row_end-1. Default value is till the last row of the dataframe.
  • col_start: The column index from which selection is started. Default is 0.
  • col_end: The column index at which selection should be end i.e. select till end-1. Default value is till the last column of the dataframe.
import pandas as sc
# List of Tuples
players = [('Messi',35, 'Barcelona',   175) ,
            ('James',25, 'Tonga' ,   187) ,
            ('Hardik', 30, 'Mumbai',   169) ,
            ('Harsh',32, 'Mumabi',   201)]
# Creation of DataFrame object
dataf = sc.DataFrame(  players,
                    columns=['Name', 'Age', 'Team', 'Height'],
                    index = ['a', 'b', 'c', 'd'])
print("Original dataframe is : ")
print(dataf)
# Select last row except last row i.e. drops it
dataf = dataf.iloc[:-1 , :]
print("Modified Dataframe is : ")
print(dataf)
Output :
Original dataframe is :
Name  Age       Team  Height
a   Messi   35  Barcelona     175
b   James   25      Tonga     187
c  Hardik   30     Mumbai     169
d   Harsh   32     Mumabi     201
Modified Dataframe is :
Name  Age       Team  Height
a   Messi   35  Barcelona     175
b   James   25      Tonga     187
c  Hardik   30     Mumbai     169

Use drop() to remove last row of pandas dataframe :

drop() function deleted a sequence of rows. Only we have to use axis=0 & pass argument inplace=True.

import pandas as sc
# List of Tuples
players = [('Messi',35, 'Barcelona',   175) ,
            ('James',25, 'Tonga' ,   187) ,
            ('Hardik', 30, 'Mumbai',   169) ,
            ('Harsh',32, 'Mumabi',   201)]
# Creation of DataFrame object
dataf = sc.DataFrame(  players,
                    columns=['Name', 'Age', 'Team', 'Height'],
                    index = ['a', 'b', 'c', 'd'])
print("Original dataframe is : ")
print(dataf)
# Drop the last row
dataf.drop(index=dataf.index[-1], 
        axis=0, 
        inplace=True)
print("Modified Dataframe is: ")
print(dataf)
Output :
Original dataframe is :
Name  Age       Team  Height
a   Messi   35  Barcelona     175
b   James   25      Tonga     187
c  Hardik   30     Mumbai     169
d   Harsh   32     Mumabi     201
Modified Dataframe is:
Name  Age       Team  Height
a   Messi   35  Barcelona     175
b   James   25      Tonga     187
c  Hardik   30     Mumbai     169

Use head() function to drop last row of pandas dataframe :

dataframe in Python provide head(n) function which returns first ‘n’ rows of dataframe. So to the delete last row of dataframe we have to only select first (n-1) rows using head() function.

import pandas as sc
# List of Tuples
players = [('Messi',35, 'Barcelona',   175) ,
            ('James',25, 'Tonga' ,   187) ,
            ('Hardik', 30, 'Mumbai',   169) ,
            ('Harsh',32, 'Mumabi',   201)]
# Creation of DataFrame object
dataf = sc.DataFrame(  players,
                    columns=['Name', 'Age', 'Team', 'Height'],
                    index = ['a', 'b', 'c', 'd'])
print("Original dataframe is : ")
print(dataf)
# To delete last row, print first n-1 rows
dataf = dataf.head(dataf.shape[0] -1)
print("Modified Dataframe is : ")
print(dataf)
Output :
Original dataframe is :
Name  Age       Team  Height
a   Messi   35  Barcelona     175
b   James   25      Tonga     187
c  Hardik   30     Mumbai     169
d   Harsh   32     Mumabi     201
Modified Dataframe is :
Name  Age       Team  Height
a   Messi   35  Barcelona     175
b   James   25      Tonga     187
c  Hardik   30     Mumbai     169