Built with Tutify.io

    AIStudyLabs

    Pandas Drop Last N Rows From DataFrame

    Pandas Drop Last N Rows From DataFrame

    To drop the last n rows from a Pandas DataFrame, you have several options like use iloc[], drop(), slicing[], and head() functions. Additionally, you can use the drop() function to drop rows from the DataFrame's beginning. In this article, I will explain how to drop/remove the last n rows from Pandas DataFrame.

    Key Points –

    <a id="h-quick-examples-of-dropping-last-n-rows"></a>

    Quick Examples of Dropping Last N Rows

    Following are quick examples of dropping the last n rows.

    
    # Quick examples of drop last n rows 
    
    # Example 1: Number of rows to drop
    n = 2
    
    # Example 2: By using DataFrame.iloc[] 
    # To drop last n rows
    df2 = df.iloc[:-n] 
    
    # Example 3: Using drop() function 
    # To delete last n rows
    df.drop(df.tail(n).index,inplace = True)
    
    # Example 4: Slicing last n rows
    df2 = df[:-n]
    
    # Example 5: Using DataFrame.head() 
    # To drop last n rows
    df2 = df.head(-n)
    

    First, let's create a pandas DataFrame.

    
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas"],
        'Fee' :[20000,25000,22000,24000],
        'Duration':['30days','40days','35days','60days'],
        'Discount':[1000,2300,2500,2000]
                  }
    index_labels=['r1','r2','r3','r4']
    df = pd.DataFrame(technologies,index=index_labels)
    print("DataFrame:\n", df)
    

    Yields below output.

    <a id="h-drop-last-n-rows-using-iloc"></a>

    Drop Last N Rows Using iloc[]

    We can use DataFrame.iloc[] the indexing syntax [:-n] with n as an integer to select the rows excluding the last n rows from the pandas DataFrame which results in a drop of the last n rows. You can also use iloc[] to drop rows by Index from pandas DataFrame.

    
    # By using DataFrame.iloc[] 
    # To drop last n rows
    n = 2
    df2 = df.iloc[:-n] 
    print("After dropping last n rows:\n", df2)
    

    Output.

    <a id="h-drop-last-n-rows-using-drop-method"></a>

    Drop Last N Rows Using drop() Method

    By using DataFrame.drop() method you can remove the last n rows from Pandas DataFrame. Use the index parameter to specify the indices of the last rows, and set inplace=True to apply the changes directly to the existing DataFrame.

    
    # Using drop() function 
    # To delete last n rows
    n = 3
    df.drop(df.tail(n).index,inplace = True)
    print(df)
    
    # Output:
    #    Courses    Fee Duration  Discount
    # r1   Spark  20000   30days      1000
    

    <a id="h-drop-last-n-rows-using-dataframe-slicing"></a>

    Drop Last N Rows Using DataFrame.slicing[]

    Alternatively, You can also use df[:-n] to slice the last n rows of the pandas DataFrame.

    
    # Slicing last n rows
    n = 2
    df2 = df[:-n]
    print(df2)
    
    # Output:
    #     Courses    Fee Duration  Discount
    # r1    Spark  20000   30days      1000
    # r2  PySpark  25000   40days      2300
    

    <a id="h-drop-last-n-rows-using-dataframe-head-function"></a>

    Drop Last N Rows Using DataFrame.head() Function

    You can also use df.head(-n) to delete the last n rows of pandas DataFrame. Generally, DataFrame.head() function is used to show the first n rows of a pandas DataFrame but you can pass a negative value to skip the rows from the bottom.

    
    # Using DataFrame.head()
    # To drop last n rows
    n = 2
    df2 = df.head(-n)
    print(df2)
    
    # Output:
    #     Courses    Fee Duration  Discount
    # r1    Spark  20000   30days      1000
    # r2  PySpark  25000   40days      2300
    

    <a id="h-complete-example"></a>

    Complete Example

    
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas"],
        'Fee' :[20000,25000,22000,24000],
        'Duration':['30days','40days','35days','60days'],
        'Discount':[1000,2300,2500,2000]
                  }
    index_labels=['r1','r2','r3','r4']
    df = pd.DataFrame(technologies,index=index_labels)
    print(df)
    
    # Number of rows to drop
    n = 2
    # By using DataFrame.iloc[] 
    # To drop last n rows
    df2 = df.iloc[:-n] 
    print(df2)
    
    # Number of rows to drop
    n = 1
    # Using drop() function 
    # To delete last n rows
    df.drop(df.tail(n).index,
            inplace = True)
    print(df)
    
    # Number of rows to drop
    n = 2
    # Slicing last n rows
    df2 = df[:-n]
    print(df2)
    
    #  Number of rows to drop
    n = 2
    #  Using DataFrame.head() function 
    # To drop last n rows
    df2 = df.head(-n)
    print(df2)
    

    <a id="h-faq-on-dropping-last-n-rows"></a>

    FAQ on Dropping Last N Rows

    You can use various methods. One common way is to use slicing notation directly on the DataFrame, such as df[:-n], where n represents the number of rows to drop from the end.

    You can. Utilize DataFrame.drop() with appropriate slicing to remove the desired rows. For example, df.drop(df.tail(n).index) will drop the last n rows.

    You can drop rows in place, modifying the original DataFrame by using the inplace=True parameter with the DataFrame.drop() method.

    Performance differences are typically negligible between the two methods for dropping rows. Use the method that suits your coding style and requirements best.

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

    Conclusion

    In summary, we have examined several functions to remove the last n rows from a Pandas DataFrame. Through examples, we've demonstrated the usage of DataFrame.iloc[], DataFrame.drop(), DataFrame.head(), and DataFrame.slicing[]. These techniques offer flexibility in managing DataFrame rows based on specific requirements.

    Happy Learning !!

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

    Related Articles

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

    References