You can delete the last row from the pandas DataFrame using either drop()
, iloc[]
and head()
methods. In this article, I will explain how to delete/drop the last row of data from Pandas DataFrame with examples.
drop()
method is used to remove columns or rows from DataFrame;- Use
axis
param to specify what axis you would like to remove. By defaultaxis=0
meaning to remove rows. Useaxis=1
or columns param to remove columns. - Use
inplace=True
to remove row/column in place meaning on existing DataFrame with out creating copy.
Key Points –
- The
drop()
method in Pandas can be used to delete the last row from a DataFrame by specifying the index of the row to be removed. - The
iloc[]
indexing method allows for row selection in a DataFrame, making it possible to drop the last row by excluding it through slicing. - The
head()
function can be employed to drop the last row by specifying the desired number of rows to include, effectively excluding the last row. - Both
drop()
andhead()
methods provide aninplace
parameter that, when set toTrue
, modifies the original DataFrame directly without the need to reassign the result. - Inspecting the DataFrame using
print()
or other visualization methods helps visually confirm whether the last row has been successfully deleted.
1. Quick Examples of Delete Last Row From DataFrame
If you are in a hurry, below are some quick examples of how to drop/delete the last row from Pandas DataFrame.
# Quick examples of delete last row from DataFrame
# By using iloc[] to select all rows except the last row
df2 = df.iloc[:-1 , :]
# Using drop() function to delete last row
df.drop(index=df.index[-1],axis=0,inplace=True)
# Using DataFrame.head() function to drop last row
df2 = df.head(df.shape[0] -1)
Now, let’s create a DataFrame with a few rows and columns and execute some examples and validate results. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
# Create DataFrame
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,24000],
'Duration':['30day','40days','35days','60days'],
'Discount':[1000,2300,2500,2000]
}
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount
r1 Spark 20000 30day 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 2500
r4 pandas 24000 60days 2000
2. Drop Last Row of Pandas DataFrame Using iloc[]
To drop the last row of a Pandas DataFrame using iloc[]
. By using DataFrame.iloc[]
you can drop the rows from DataFrame and use -1
to drop the last row.
# By using iloc[]
# To select all rows except the last row
df2 = df.iloc[:-1 , :]
print(df2)
In the above example, use df.iloc[:-1,:]
to select all rows except the last one and then assign it back to the original variable which ideally drops the last row from DataFrame. As a result, the last row is effectively dropped from the DataFrame. This example yields the below output.
# Output:
Courses Fee Duration Discount
r1 Spark 20000 30day 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 2500
3. Using drop() Function to Delete Last Row of Pandas DataFrame
Alternatively, you can also use drop()
method to remove the last row. Use index
param to specify the last index
and inplace=True
to apply the change on the existing DataFrame. In the below example, df.index[-1]
returns r3
which is the last row from our DataFrame.
# Using drop() function to delete last row
df.drop(index=df.index[-1],axis=0,inplace=True)
print(df)
Yields the same output as above.
4. Drop Last Row of Pandas DataFrame Using head() Function
The head()
function in Pandas returns the first n rows of a DataFrame. If you want to drop the last row using head()
, you can use it in combination with the length of the DataFrame to exclude the last row.
# Use head() to drop the last row
df = df.head(len(df) - 1)
print(df)
In the above example, df.head(len(df) - 1)
returns all rows except the last one, effectively dropping the last row from the DataFrame.
Similarly, using DataFrame.head()
in combination with DataFrame.shape[0]
to drop the last row from the DataFrame. The df.shape[0]
gives you the number of rows in the DataFrame, and by subtracting 1, you get all rows except the last one.
# Using DataFrame.head() function
# To drop last row
df2 = df.head(df.shape[0] - 1)
print(df2)
Yields the same output as above.
Frequently Asked Questions on Delete Last Row of Data of DataFrame
There are multiple ways to delete the last row from a Pandas DataFrame. Two common approaches include using the drop()
method or using iloc[]
indexing.
The head()
function can be used to exclude the last row by specifying the number of rows to include. You can subtract 1 from the total number of rows in the DataFrame.
The default behavior for these methods is to return a new DataFrame with the specified rows removed, and they do not modify the original DataFrame in place. If you want to modify the original DataFrame, you can use the inplace=True
argument.
The performance difference is likely to be negligible for small to medium-sized DataFrames. Choose the method that aligns with your coding style and preference. For very large DataFrames, performance considerations might become more relevant.
You can print the DataFrame or check its length before and after the deletion to verify that the last row has been removed.
Conclusion
In this article, you have learned how to drop the last row data of Pandas DataFrame using DataFrame.iloc[]
, DataFrame.drop()
, and DataFrame.head()
function with examples.
Happy Learning !!
Related Articles
- How to Drop Rows From Pandas DataFrame Examples
- Pandas Drop Rows With Condition
- How to Merge Series into Pandas DataFrame
- Change the Order of Pandas DataFrame Columns
- How to Combine Two Series into pandas DataFrame
- Get Count of Each Row of Pandas DataFrame
- Get Unique Rows in Pandas DataFrame
- Get First N Rows of Pandas DataFrame
- Pandas Drop First Three Rows From DataFrame
- Pandas.Index.drop_duplicates() Explained
- Apply Multiple Filters to Pandas DataFrame or Series
- Append Pandas DataFrames Using for Loop