• Post author:
  • Post category:Pandas
  • Post last modified:October 4, 2024
  • Reading time:13 mins read
You are currently viewing 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.

Advertisements

Key Points –

  • Utilize the drop() method is used to remove columns or rows from DataFrame.
  • You can drop the first row by slicing the DataFrame with .iloc[].
  • Use ignore_index=True to reset the index when dropping the first row.
  • 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.
  • After dropping the first row, reindex the DataFrame if you want to reset the index.
  • Use inplace=True to remove row/column in place meaning on existing DataFrame without creating a copy.

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)

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.

Pandas drop first row

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("The DataFrame after dropping the first row:\n", df1)

Yields below output.

Pandas drop first row

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

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

FAQ on Drop the First Row

How do I drop the first row of a DataFrame using the drop method?

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

What if I want to drop multiple rows, including the first row?

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.

Are there other methods to drop the first row of a DataFrame?

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.

Is there a way to drop the first row using 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.

Is it possible to reset the index after dropping the first row?

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.

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 !!

References