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:
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:Â
Â
Â
Â
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: Python3 # 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
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: Python3 # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict = {'Name': ['Sumit Tyagi', 'Sukritin', 'Akriti Goel', 'Sanskriti'
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 access
4 min read