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

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

Advertisements

Key Points

  • The tail() method can be used to return the remaining rows after excluding the first N rows, based on the DataFrame’s total row count.
  • When using drop(), you can modify the DataFrame in place by setting inplace=True, which avoids creating a new object.
  • Dropping rows is generally efficient, but when working with large DataFrames, consider performance trade-offs when using methods like iloc[] or drop().
  • Using negative numbers in iloc[] and tail() is not valid for dropping rows; slicing with positive numbers should be used instead.
  • Most Pandas operations like slicing and dropping return a new DataFrame by default, so the original DataFrame remains unchanged unless modified in place.

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

Quick Examples of Drop First N Rows

Following are quick examples of dropping 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)

First, let’s create a Pandas DataFrame.


# 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

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

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 the axis parameter to specify which axis you would like to delete. By default, axis=0 means deleting rows. To delete columns, use axis=1 or the columns parameter.
  • Use inplace=True to delete a row or column directly on the existing DataFrame without creating a copy. This modifies the DataFrame object itself.

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

# Output:
#     Courses    Fee Duration  Discount
# r3  Python  22000   35days      2500
# r4  pandas  24000   60days      2000

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)

# Output:
#     Courses    Fee Duration  Discount
# r3  Python  22000   35days      2500
# r4  pandas  24000   60days      2000

Complete Example

Below is a complete example of dropping the first N rows.


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)

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

Conclusion

In conclusion, we’ve explored various methods to drop the first n rows from a Pandas DataFrame. We discussed using DataFrame.iloc[], DataFrame.drop(), and DataFrame.tail() functions, each offering a different approach to achieve the same result. Additionally, we highlighted the importance of the axis parameter and introduced the inplace=True option for modifying the DataFrame object directly.

Happy Learning !!

References

Leave a Reply