• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing Pandas Drop First N Rows From DataFrame

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

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

1. Quick Examples of Drop First N Rows From Pandas DataFrame

If you are in a hurry, below are some quick examples of how to drop 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)

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


# 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.

Pandas Drop First N Rows

2. 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.

Pandas Drop First N Rows

3. Delete the Top N Rows of DataFrame Using drop()

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

# Using drop() function to delete first n rows
n = 2
df.drop(index=df.index[:n], inplace=True)
print(df)

Yields the same output as above.

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

Yields the same output as above.

5. Complete Example For Drop First N Rows From DataFrame

Below is a complete example of dropping the first N rows from pandas 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(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)

Frequently Asked Questions on Pandas Drop First N Rows From DataFrame

How can I drop the first N rows from a Pandas 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.

Can I drop the first N rows in place?

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.

Is there an alternative to dropping rows 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.

What if I want to drop rows based on a condition instead of a fixed number?

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).

How can I drop the first N rows from a Pandas DataFrame?

You can drop the first N rows from a Pandas DataFrame using the drop method along with the index parameter.

Can I modify the original DataFrame in place instead of creating a new one?

You can modify the original DataFrame in place by using the inplace=True parameter with the drop method. Setting inplace=True modifies the original DataFrame (df) without the need to create a new one.

Conclusion

In this article, you have learned how to drop the first n rows From Pandas DataFrame using DataFrame.iloc[], DataFrame.drop() and Dataframe.tail() function with examples.

Happy Learning !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply