Built with Tutify.io

    AIStudyLabs

    Pandas Drop the First Row of DataFrame

    Pandas Drop the First Row of DataFrame

    You can remove the first row from a Pandas DataFrame using methods such as drop(), iloc[], and tail() functions. In this article, I will explain drop the first row from a Pandas DataFrame.

    Key Points –

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

    Quick Examples of Drop First Row

    Following are quick examples of how to drop the first row of DataFrame.

    
    # Quick examples of dropping first row
    
    # Example 1: Use DataFrame.iloc[] 
    # To drop first row 
    df1 = df.iloc[1:]
    
    # Example 2: Use DataFrame.drop()
    # To delete first row
    df.drop(index=df.index[0], axis=0, inplace=True)
    
    # Example 3: Use DataFrame.tail()
    # To drop first row
    df1 = df.tail(-1)
    

    <a id="h-usage-of-pandas-drop-the-first-row-of-dataframe"></a>

    Usage of Pandas Drop the First Row of DataFrame

    To drop the first row in a Pandas DataFrame, you can use the DataFrame.iloc or DataFrame.drop() method.

    Now, let's create a DataFrame with a few rows and columns, execute these examples, and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.

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

    Yields below output.

    <a id="h-pandas-drop-first-row-using-iloc"></a>

    Pandas Drop First Row using iloc[]

    By using DataFrame.iloc[] attribute you can drop the first row of pandas DataFrame. Actually, this function selects the particular portion of a given DataFrame based on its indices and ignores the rest of the portion. The particular portion may be a few rows or a few columns.

    For example, use this syntax DataFrame.iloc[1:] to select all rows except the first row and then assign it back to the original variable which ideally drops the first row from DataFrame. We can also get the first row of DataFrame and get the last row of DataFrameusing the Pandas iloc[] property.

    
    # Use DataFrame.iloc[] to drop first row 
    df1 = df.iloc[1:]
    print("The DataFrame after dropping the first row:\n", df1)
    

    Yields below output.

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

    Use DataFrame.drop() to Delete First Row

    In pandas, DataFrame.drop() method is used to drop rows from the DataFrame. To remove rows use axis=0 param to the drop() function. Use the index param to specify which row you want to delete from the DataFrame and inplace=True to apply the change to the existing DataFrame.

    
    # Drop first row using drop()
    df.drop(index=df.index[0], axis=0, inplace=True)
    print("The DataFrame after dropping the first row:\n", df)
    
    # Output:
    # The DataFrame after dropping the first row:
    #     Courses    Fee Duration  Discount
    # r2  PySpark  25000   40days      2300
    # r3   Python  22000   35days      2500
    # r4   pandas  24000   60days      2000
    # r5   Hadoop  30000   55days      3000
    

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

    Use DataFrame.tail() Method to Drop First Row

    In Python, Pandas DataFrame provides a tail() function that is used to return the last n rows of the DataFrame. So, we can delete the first row of DataFrame by using df.tail(-1), it will select all rows except the first row of DataFrame.

    
    # Use DataFrame.tail() method to drop first row
    df1 = df.tail(-1)
    print("The DataFrame after dropping the first row:\n", df1)
    
    # Output:
    # The DataFrame after dropping the first row:
    #     Courses    Fee Duration  Discount
    # r2  PySpark  25000   40days      2300
    # r3   Python  22000   35days      2500
    # r4   pandas  24000   60days      2000
    # r5   Hadoop  30000   55days      3000
    

    <a id="h-faq-on-drop-the-first-row"></a>

    FAQ on Drop the First Row

    To drop the first row using the drop method, you can specify the index of the row to be dropped.

    If you want to drop multiple rows, including the first row, you can use the drop method and specify a list of indices to drop.

    There are several methods to drop the first row of a DataFrame in Pandas. In addition to using DataFrame.drop and DataFrame.iloc, another approach is to use the tail() method.

    The tail method in Pandas is typically used to view the last n rows of a DataFrame. However, you can indirectly drop the first row using the tail method by specifying a negative number for the number of rows to display. When you specify -1 as the argument to tail, it displays all rows except the first one.

    After dropping the first row from a DataFrame, you can reset the index using the reset_index method. This method resets the index of the DataFrame to the default integer index, starting from 0, and adds a new column containing the old index values if needed.

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

    Conclusion

    In conclusion, this article demonstrated various methods to drop the first row from a Pandas DataFrame. You explored using the iloc[], drop(), and tail() methods, providing practical examples for each approach.

    Happy Learning !!

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

    Related Articles

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

    References