Pandas Drop First N Rows From DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:November 10, 2023

Use iloc[], drop() and tail() methods to drop the top/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.


# Below are the quick examples

# Number of rows to drop
n = 2

# By using DataFrame.iloc[] to drop first n rows
df2 = df.iloc[n:,:]

# Using iloc[] to drop first n rows
df2 = df.iloc[n:]

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

# Using DataFrame.tail() to Drop top two rows
df2 = df.tail(df.shape[0] -n)

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


# By using DataFrame.iloc[] to drop first n rows
n = 2
df2 = df.iloc[n:]
print(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)

5. Complete Example For Drop First N Rows From DataFrame

Yields the same output as above.

Below is a complete example of Dropping Top 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)

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

You are currently viewing Pandas Drop First N Rows From DataFrame