In this tutorial, we will look at how to drop the last n rows of a pandas dataframe.
How to drop the last n rows of a dataframe?
There are a number of ways to remove the last n rows of a dataframe. For example, you can slice the dataframe using .iloc
or you can use the pandas drop()
function or you can use the pandas head()
function. The following is the syntax for the different methods used in this tutorial.
# using iloc df.iloc[:-n] # using drop() df.drop(df.index[-n:]) # using head() df.head(-n)
Examples
Now let’s look at the usage of the above different methods with the help of some examples. First, we will create a sample dataframe that we will be using throughout this tutorial.
import pandas as pd # create dataframe df = pd.DataFrame({ 'Name': ['Bulbasaur', 'Charmander', 'Squirtle', 'Caterpie', 'Ekans'], 'Type': ['Grass', 'Fire', 'Water', 'Bug', 'Poison'] }) # display the dataframe print(df)
Output:
Name Type 0 Bulbasaur Grass 1 Charmander Fire 2 Squirtle Water 3 Caterpie Bug 4 Ekans Poison
We now have a dataframe with five rows containing information on Pokemon names and their respective types.
1. Remove the last n rows with .iloc
You can drop the last n rows of a dataframe by slicing it using .iloc
. For example, let’s remove the last two rows of df and store the resulting dataframe as a separate dataframe.
# remove last two rows df_sub = df.iloc[:-2] # display the dataframe print(df_sub)
Output:
Name Type 0 Bulbasaur Grass 1 Charmander Fire 2 Squirtle Water
The resulting dataframe has the bottom two rows removed. Note that, the slice :-2
refers to take all the rows starting from index 0 till but not including the 2nd row from the bottom.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
2. Remove the last n rows with drop()
You can also use the pandas drop()
function to remove the last n rows of a dataframe. For this, pass the indexes of the rows you want to delete to the drop() function. For example, let’s again drop the last two rows of the dataframe df, this time using the drop() function.
# remove last two rows df_sub = df.drop(df.index[-2:]) # display the dataframe print(df_sub)
Output:
Name Type 0 Bulbasaur Grass 1 Charmander Fire 2 Squirtle Water
The last two rows are dropped in the returned dataframe. If you want to modify the original dataframe in place, pass inplace=True
to the drop() function.
3. Remove last n rows with head()
You can also use the pandas head()
function to remove the last n rows. Generally, the head() function is used to show the first n rows of a pandas dataframe but you can pass a negative value to skip the rows from the bottom.
# remove last two rows df_sub = df.head(-2) # display the dataframe print(df_sub)
Output:
Name Type 0 Bulbasaur Grass 1 Charmander Fire 2 Squirtle Water
You can see that the resulting dataframe has all the rows from the top except for the bottom two rows. This is because we passed -2 to the head() function which equates to performing df[:-n]
.
For more on the pandas head() function, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on removing data from pandas dataframe –