Built with Tutify.io

    AIStudyLabs

    Pandas Drop First N Rows From DataFrame

    Pandas Drop First N Rows From DataFrame

    In Pandas, you can use the drop() function to remove the top/first N rows from a DataFrame. Use iloc[], drop() and tail() functions to drop the first n rows from the pandas DataFrame. In this article, I will explain drop/delete the first n rows from Pandas DataFrame.

    Key Points –

    Related: You can also drop the last N rows from DataFrame.

    <a id="h-quick-examples-of-drop-first-n-rows"></a>

    Quick Examples of Drop First N Rows

    Following are quick examples of dropping the first n rows from Pandas DataFrame.

    
    # Quick examples of drop first N rows 
    
    # Number of rows to drop
    n = 2
    
    # Example 1: By using Dataframe.iloc[] 
    # To drop first n rows
    df2 = df.iloc[n:,:]
    
    # Example 2: Using iloc[] 
    # To drop first n rows
    df2 = df.iloc[n:]
    
    # Example 3: Using drop() function 
    # To delete first n rows
    df.drop(index=df.index[:n], axis=0, inplace=True)
    
    # Example 4: Using DataFrame.tail() 
    # To Drop top two rows
    df2 = df.tail(df.shape[0] -n)
    
    # Example 5: Using DataFrame.tail() function 
    # To drop first n rows
    df2 = df.tail(-2)
    

    <a id="h-usage-of-drop-first-n-rows-from-pandas-dataframe"></a>

    Usage of Drop First N Rows From Pandas DataFrame

    You can drop the first N rows of a pandas DataFrame using the df.iloc accessor or the df[n:] slicing method.

    First, let's create a Pandas DataFrame.

    
    # Create 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("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-using-iloc-to-drop-first-n-rows-of-dataframe"></a>

    Using iloc[] to Drop First N Rows of DataFrame

    Use DataFrame.iloc[] the indexing syntax [n:] with n as an integer to select the first n rows from pandas DataFrame. For example df.iloc[n:], substitute n with the integer number specifying how many rows you want to delete.

    
    # Using DataFrame.iloc[] 
    # To drop first n rows
    n = 2
    df2 = df.iloc[n:]
    print("After dropping first n rows:\n", df2)
    
    # Using iloc[] to drop first n rows
    df2 = df.iloc[2:]
    print("After dropping first n rows:\n", df2)
    

    Yields below output.

    <a id="h-delete-the-top-n-rows-of-dataframe-using-drop"></a>

    Delete the Top N Rows of DataFrame Using drop()

    
    # Using drop() function 
    # To delete first n rows
    n = 2
    df.drop(index=df.index[:n], inplace=True)
    print(df)
    
    # Output:
    #     Courses    Fee Duration  Discount
    # r3  Python  22000   35days      2500
    # r4  pandas  24000   60days      2000
    

    <a id="h-remove-first-n-rows-of-pandas-dataframe-using-tail"></a>

    Remove First N Rows of Pandas DataFrame Using tail()

    Alternatively, you can also use df.tail(df.shape[0] -n) to remove the top/first n rows of pandas DataFrame. Generally, the DataFrame.tail() function is used to show the last n rows of a pandas DataFrame but you can pass a negative value to skip the rows from the beginning.

    
    # Number of rows to drop
    n = 2
    
    # Using DataFrame.tail() 
    # To Drop top two rows
    df2 = df.tail(df.shape[0] -n)
    print(df2)
    
    # Using DataFrame.tail() function 
    # To drop first n rows
    df2 = df.tail(-2)
    print(df2)
    
    # Output:
    #     Courses    Fee Duration  Discount
    # r3  Python  22000   35days      2500
    # r4  pandas  24000   60days      2000
    

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

    Complete Example

    Below is a complete example of dropping the first N rows.

    
    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 first n rows
    df2 = df.iloc[n:,:]
    print(df2)
    
    # Using iloc[] 
    # To drop first n rows
    df2 = df.iloc[2:]
    print(df2)
    
    # Number of rows to drop
    n = 2
    
    # Using drop() function 
    # To delete first n rows
    df.drop(index=df.index[:n],axis=0, inplace=True)
    print(df)
    
    # Number of rows to drop
    n = 2
    
    # Using DataFrame.tail() 
    # To Drop top two rows
    df2 = df.tail(df.shape[0] -n)
    print(df2)
    
    # Using DataFrame.tail() function 
    # To drop first n rows
    df2 = df.tail(-2)
    print(df2)
    

    <a id="h-faq-on-drop-first-n-rows-from-dataframe"></a>

    FAQ on Drop First N Rows from DataFrame

    You can drop the first N rows from a DataFrame using the drop method or the iloc method. With drop, you can specify the indices of the rows to be removed, while with iloc, you can select the rows based on integer-location indexing.

    You can drop the first N rows in place using the inplace=True parameter with the drop method or by directly modifying the DataFrame using iloc.

    Another alternative to dropping rows is using the tail() method. You can use it in combination with iloc to exclude the last N rows, effectively removing the first N rows.

    If you want to drop rows based on a condition, you can use boolean indexing. Here's an example where rows are dropped based on a condition (e.g., dropping rows where column 'A' is less than 3).

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

    Conclusion

    In conclusion, we've explored various methods to drop the first n rows from a Pandas DataFrame. We discussed using DataFrame.iloc[], DataFrame.drop(), and DataFrame.tail() functions, each offering a different approach to achieve the same result. Additionally, we highlighted the importance of the axis parameter and introduced the inplace=True option for modifying the DataFrame object directly.

    Happy Learning !!

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

    Related Articles

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

    References