Pandas Drop the First Row of DataFrame

You can delete/drop the first row from the Pandas DataFrame using either drop()iloc[] and tail() methods. In this article, I will explain how to delete/drop the first row from Pandas DataFrame with examples.

  • drop() method is used to remove columns or rows from DataFrame.
  • Use the axis param to specify what axis you would like to remove. By default axis = 0 meaning to remove rows. Use axis=1 or columns param to remove columns.
  • Use inplace=True to remove row/column in place meaning on existing DataFrame without creating a copy.

1. Quick Examples of Dropping First Row from DataFrame

If you are in a hurry, below are some quick examples of how to drop the first row of DataFrame.


# Below are quick example
# Example 1: Use DataFrame.iloc[] to drop first row 
df1 = df.iloc[1:]
 
# Example 2: Use DataFrame.drop() function to delete first row
df.drop(index=df.index[0], axis=0, inplace=True)

# Example 3:  Use DataFrame.tail() method to drop first row
df1 = df.tail(-1)

Let’s create DataFrame using data from the Python dictionary and run the above examples to get the first row of DataFrame.


# 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(df)


# Outputs:
#     Courses    Fee Duration  Discount
# r1    Spark  20000    30day      1000
# r2  PySpark  25000   40days      2300
# r3   Python  22000   35days      2500
# r4   pandas  24000   60days      2000
# r5   Hadoop  30000   55days      3000

2. 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 DataFrame using the Pandas iloc[] property.


# Use DataFrame.iloc[] to drop first row 
df1 = df.iloc[1:]
print(df1) 

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r4   pandas  24000   60days      2000
r5   Hadoop  30000   55days      3000

3. Use DataFrame.drop() to Delete First Row

In pandas, DataFrame.drop() function 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(df)

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r4   pandas  24000   60days      2000
r5   Hadoop  30000   55days      3000

4. 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(df1)

Yields the same output as above.

5. Conclusion

In this article, you have learned how to drop the first row from Pandas DataFrame using DataFrame.iloc[], DataFrame.drop(), and DataFrame.tail() functions with examples.

Happy Learning !!

References

Leave a Reply

You are currently viewing Pandas Drop the First Row of DataFrame