How to delete the last row of data of a pandas DataFrame?

Given a Pandas DataFrame, we have to delete the last row of data of it. By Pranit Sharma Last updated : September 22, 2023

Rows in pandas are the different cell (column) values that are aligned horizontally and also provide uniformity. Each row can have the same or different value. Rows are generally marked with the index number but in pandas, we can also assign index names according to the needs. In pandas, we can create, read, update and delete a column or row value.

An index is the number of rows that ranges from 0 to n-1, so if the index is 0, it represents the first row and the n-1th index represents the last row.

Problem statement

Given a Pandas DataFrame, we have to delete the last row of data of it.

Deleting the last row of data of a pandas DataFrame

To delete the last row of the pandas DataFrame, we will first access the last index of the DataFrame by subtracting 1 from the length of the DataFrame. We will then remove the last index of the DataFrame with the help of pandas.DataFrame.drop() method.

Note

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

Python code to delete the last row of data of a pandas DataFrame

# Importing pandas package
import pandas as pd

# Creating a dictionary
dict = {
    'Name':[
        'Harry','Raman','Parth','Mukesh','Neelam','Megha',
        'Deepak','Nitin','Manoj','Rishi','Sandeep','Divyansh',
        'Sheetal','Shalini'
        ],
    'Sport_selected':[
        'Cricket','Cricket','Cricket','Cricket','Basketball',
        'Basketball','Football','Cricket','Tennis','Tennis',
        'Chess','Football','Basketball','Chess'
        ]
}

# Creating a DataFrame
df=pd.DataFrame(dict)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Accessing the last row
last_row = len(df)

# Deleting the last row
result = df.drop(df.index[last_row-1])

# Display result
print("DataFrame after deleting the last row:\n",result)

Output

The output of the above program is:

Original DataFrame:
         Name Sport_selected
0      Harry        Cricket
1      Raman        Cricket
2      Parth        Cricket
3     Mukesh        Cricket
4     Neelam     Basketball
5      Megha     Basketball
6     Deepak       Football
7      Nitin        Cricket
8      Manoj         Tennis
9      Rishi         Tennis
10   Sandeep          Chess
11  Divyansh       Football
12   Sheetal     Basketball
13   Shalini          Chess 

DataFrame after deleting the last row:
         Name Sport_selected
0      Harry        Cricket
1      Raman        Cricket
2      Parth        Cricket
3     Mukesh        Cricket
4     Neelam     Basketball
5      Megha     Basketball
6     Deepak       Football
7      Nitin        Cricket
8      Manoj         Tennis
9      Rishi         Tennis
10   Sandeep          Chess
11  Divyansh       Football
12   Sheetal     Basketball

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.