Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Get first N records in Pandas DataFrame
Next article icon

Remove last n rows of a Pandas DataFrame

Last Updated : 29 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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: 
 


 


 



Next Article
Get first N records in Pandas DataFrame

S

sukritinpal
Improve
Article Tags :
  • Python
  • Python pandas-basics
Practice Tags :
  • python

Similar Reads

  • Get last n records of a Pandas DataFrame
    Let's discuss how to get last n records of a Pandas DAtaframe. There can be various methods to get the last n records of a Pandas DataFrame. Lets first make a dataframe:Example: C/C++ Code # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict
    2 min read
  • Drop a list of rows from a Pandas DataFrame
    Let us see how to drop a list of rows in a Pandas DataFrame. We can do this using the Pandas drop() function. We will also pass inplace = True and axis=0 to denote row, as it makes the changes we make in the instance stored in that instance without doing any assignment. Creating Dataframe to drop a
    3 min read
  • How to Reverse Row in Pandas DataFrame?
    In this article, we will learn how to reverse a row in a pandas data frame using Python.  With the help of Pandas, we can perform a reverse operation by using loc(), iloc(), reindex(), slicing, and indexing on a row of a data set.  Creating Dataframe Let’s create a simple data frame with a dictionar
    3 min read
  • Get first N records in Pandas DataFrame
    When working with large datasets in Python using the Pandas library, it is often necessary to extract a specific number of records from a column to analyze or process the data, such as the first 10 values from a column. For instance, if you have a DataFrame df with column A, you can quickly get firs
    5 min read
  • Remove infinite values from a given Pandas DataFrame
    Let's discuss how to Remove the infinite values from the Pandas dataframe. First let's make a dataframe: Example: C/C++ Code # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict = {'Name': ['Sumit Tyagi', 'Sukritin', 'Akriti Goel', 'Sanskrit
    2 min read
  • How to get nth row in a Pandas DataFrame?
    Pandas Dataframes are basically table format data that comprises rows and columns. Now for accessing the rows from large datasets, we have different methods like iloc, loc and values in Pandas. The most commonly used method is iloc(). Let us consider a simple example. Method 1. Using iloc() to acces
    4 min read
  • How to Get First Row of Pandas DataFrame?
    To get the first row of a Pandas Dataframe there are several methods available, each with its own advantages depending on the situation. The most common methods include using .iloc[], .head(), and .loc[]. Let's understand with this example: [GFGTABS] Python import pandas as pd data = {'Name'
    4 min read
  • How to remove random symbols in a dataframe in Pandas?
    In this article, we will see how to remove random symbols in a dataframe in Pandas. Method 1: Selecting columns Syntax: dataframe[columns].replace({symbol:},regex=True) First, select the columns which have a symbol that needs to be removed. And inside the method replace() insert the symbol example r
    2 min read
  • How to add one row in existing Pandas DataFrame?
    Adding rows to a Pandas DataFrame is a common task in data manipulation and can be achieved using methods like loc[], and concat(). Method 1. Using loc[] - By Specifying its Index and ValuesThe loc[] method is ideal for directly modifying an existing DataFrame, making it more memory-efficient compar
    4 min read
  • Creating views on Pandas DataFrame
    Many times while doing data analysis we are dealing with a large data set, having a lot of attributes. All the attributes are not necessarily equally important. As a result, we want to work with only a set of columns in the dataframe. For that purpose, let's see how we can create views on the Datafr
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences