ADVERTISEMENT
ADVERTISEMENT

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.
Submitted by Pranit Sharma, on June 12, 2022

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.

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.

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:

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.