Built with Tutify.io

    AIStudyLabs

    Pandas Delete Last Row From DataFrame

    Pandas Delete Last Row From DataFrame

    To delete the last row from a pandas DataFrame, you can use the drop() method with the index of the last row. In this article, I will explain how to remove the last row of data from DataFrame by using either drop(), iloc[], or head() functions.

    Key Points –

    <a id="h-quick-examples-of-delete-last-row"></a>

    Quick Examples of Delete Last Row

    Following are quick examples of deleting the last row from Pandas DataFrame.

    
    # Quick examples of delete last row
    
    # 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)
    

    To run some examples of deleting the last row from Pandas DataFrame, let’s create Pandas DataFrame using data from a dictionary.

    
    # 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
    

    <a id="h-drop-last-row-of-pandas-dataframe-using-iloc"></a>

    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.

    
    # Output:
        Courses    Fee Duration  Discount
    r1    Spark  20000    30day      1000
    r2  PySpark  25000   40days      2300
    r3   Python  22000   35days      2500
    

    <a id="h-delete-last-row-of-pandas-dataframe-using-drop"></a>

    Delete Last Row of Pandas DataFrame Using drop()

    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.

    <a id="h-drop-last-row-of-pandas-dataframe-using-head"></a>

    Drop Last Row of Pandas DataFrame Using head()

    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.

    <a id="h-frequently-asked-questions-on-delete-last-row-of-data"></a>

    Frequently Asked Questions on Delete Last Row of Data

    You can print the DataFrame or check its length before and after the deletion to verify that the last row has been removed.

    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 drop method is irreversible unless you have a backup of the DataFrame or are working with a copy. Ensure to review and confirm the deletion before executing the operation.

    ou can use slicing to exclude the last row or iloc to select all rows except the last one. However, the drop method offers more flexibility for general row or column deletion.

    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.

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, removing the last row from a Pandas DataFrame is essential for data manipulation tasks. Through this article, you've gained insights into three different methods for accomplishing this task: DataFrame.iloc[], DataFrame.drop(), and DataFrame.head(). Understanding these methods equips you with the flexibility to choose the most suitable approach based on your specific requirements.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References