Built with Tutify.io

    AIStudyLabs

    Pandas - Drop First Three Rows From DataFrame

    Pandas - Drop First Three Rows From DataFrame

    To drop the first three rows from a Pandas DataFrame, you can use the drop() function with the index parameter set to a list containing the row indices you want to remove.

    In this article, I will explain dropping the first three rows from DataFrame by using either drop(), iloc[], or tail() functions.

    Key Points –

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

    Quick Examples of Drop First Three Rows

    Below are some quick examples of how to drop the first two/three rows from DataFrame.

    
    # Quick examples of drop first three rows 
    
    # Use DataFrame.iloc[]
    # To drop first three rows 
    df2 = df.iloc[3:]
    
    # Use DataFrame.iloc[]
    n = 3
    df2 = df.iloc[n:]
    
    # Use DataFrame.drop() function 
    # To delete first three rows
    df.drop(df.head(3).index, inplace=True)
    
    # Use DataFrame.drop() function
    df.drop(df.index[:3], inplace=True)
    
    # Use DataFrame.tail() method 
    # To drop first three rows
    df2 = df.tail(-3)
    

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

    Usage of Drop First Three Rows From Pandas DataFrame

    To drop the first three rows from a Pandas DataFrame, you can use the iloc indexer or df[3:] slicing.

    To run some examples of dropping the first three rows 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","Hadoop"],
        'Fee' :[20000,25000,22000,24000,30000],
        'Duration':['30day','40days','35days','60days','55days'],
        'Discount':[1000,2300,2500,2000,3000]
                  }
    index_labels=['r1','r2','r3','r4','r5']
    df = pd.DataFrame(technologies,index=index_labels)
    print("Original DataFrame\n",df)
    

    Yields below output.

    <a id="h-use-dataframe-iloc-to-remove-first-three-rows"></a>

    Use DataFrame.iloc[] to Remove First Three Rows

    By using DataFrame.iloc[] you can drop the rows from DataFrame and use 3: it to drop the first three rows. For instance, used DataFrame.iloc[3:] to filter and select all rows except the first three, effectively removing those initial rows from the DataFrame, and then reassigning the resulting DataFrame back to the original variable.

    
    # Use DataFrame.iloc[]
    # To drop first three rows 
    df2 = df.iloc[3:]
    print(df2) 
    
    # Use DataFrame.iloc[]
    n = 3
    df2 = df.iloc[n:]
    print(df2)
    
    # Output:
    #    Courses    Fee Duration  Discount
    # r4  pandas  24000   60days      2000
    # r5  Hadoop  30000   55days      3000
    

    <a id="h-use-dataframe-drop-to-delete-first-three-rows"></a>

    Use DataFrame.drop() to Delete First Three Rows

    Alternatively, you can also utilize the DataFrame.drop() method to eliminate the first three rows. Specify the first index using the index parameter and set inplace=True to apply the change directly to the existing DataFrame. For instance, df.drop(df.head(3).index, inplace=True) method.

    
    # Use DataFrame.drop() function 
    # To delete first three rows
    df.drop(df.head(3).index, inplace=True)
    print(df) 
    
    # Use DataFrame.drop() function
    df.drop(df.index[:3], inplace=True)
    print(df)
    
    # Output:
    #    Courses    Fee Duration  Discount
    # r4  pandas  24000   60days      2000
    # r5  Hadoop  30000   55days      3000
    

    <a id="h-use-dataframe-tail-method-to-drop-first-three-rows"></a>

    Use DataFrame.tail() Method to Drop First Three Rows

    The DataFrame.tail() method is typically used to retrieve the last n rows of a DataFrame. However, we can manipulate it slightly to drop the first three rows by combining it with the negative indexing feature.

    
    # Use DataFrame.tail() method
    # To drop first three rows
    df2 = df.tail(-3)
    print(df2) 
    
    # Output:
    #    Courses    Fee Duration  Discount
    # r4  pandas  24000   60days      2000
    # r5  Hadoop  30000   55days      3000
    

    <a id="h-complete-example-for-drop-first-three-rows"></a>

    Complete Example for Drop First Three Rows

    
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
        'Fee' :[20000,25000,22000,24000,30000],
        'Duration':['30day','40days','35days','60days','55days'],
        'Discount':[1000,2300,2500,2000,3000]
                  }
    index_labels=['r1','r2','r3','r4','r5']
    df = pd.DataFrame(technologies,index=index_labels)
    print(df)
    
    # Use DataFrame.iloc[] 
    # To drop first three rows 
    df2 = df.iloc[3:]
    print(df2) 
    
    # Use DataFrame.iloc[]
    n = 3
    df2 = df.iloc[n:]
    print(df2)
    
    # Use DataFrame.drop() function 
    # To delete first three rows
    df.drop(df.head(3).index, inplace=True)
    print(df) 
    
    # Use DataFrame.drop() function
    df.drop(df.index[:3], inplace=True)
    print(df) 
    
    # Use DataFrame.tail() method 
    # To drop first three rows
    df2 = df.tail(-3)
    print(df2)
    

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

    FAQ on Drop First Three Rows From Pandas DataFrame

    To drop the first three rows from a Pandas DataFrame, you can use either iloc[] or drop().

    You can use the drop() method by specifying the indices of the rows you want to drop.

    If you don't reset the index, the DataFrame will retain the original indices of the rows that are left after dropping the first three. This can lead to non-sequential index values.

    By default, the drop() method and iloc[] method do not modify the original DataFrame in place. To modify the original DataFrame, you can either assign the result back to the original DataFrame or use the inplace=True parameter with drop().

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

    Conclusion

    In this article, you have learned about various methods for dropping the first three rows from a Pandas DataFrame. We explored techniques such as iloc[], drop(), and even repurposed tail() to achieve this task, supported by practical examples.

    Happy Learning !!

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

    Related Articles

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

    References