Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Remove last n rows of a Pandas DataFrame

  • Last Updated : 29 Jul, 2021

Let’s see the various methods to Remove last n rows of a Pandas Dataframe.
First, let’s make a dataframe:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Python3




# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Print Dataframe
print(df)

 



 

Output: 

 

 

Method 1: Using Dataframe.drop() .
We can remove the last n rows using the drop() method. drop() method gets an inplace argument which takes a boolean value. If inplace attribute is set to True then the dataframe gets updated with the new value of dataframe (dataframe with last n rows removed).

 

Example:



 

Python3




# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Dropping last n rows using drop
df.drop(df.tail(n).index,
        inplace = True)
 
# Printing dataframe
print(df)

Output: 

 

Method 2: Using Dataframe.iloc[ ].

This method is used when the index label of a data frame is something other than numeric series of 0, 1, 2, 3….n or in case the user doesn’t know the index label. 

Example:

Python3




# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Removing last n rows
df_dropped_last_n = df.iloc[:-n]
 
# Printing dataframe
print(df_dropped_last_n)

 



 

Output: 

 

 

 

 

Method 3: Using Dataframe.head().

 

This method is used to return top n (5 by default) rows of a data frame or series.

 

Example:

 

Python3




# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Using head() to
# drop last n rows
df1 = df.head(-n)
 
# Printing dataframe
print(df1)

Output: 

 

Method 4: Using Dataframe slicing [ ].

Example:

Python3




# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Slicing last n rows
df1 = df[:-n]
 
# Printing dataframe
print(df1)

 

 

Output: 
 

 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!