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.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
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.
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 –