Pandas Drop First N Rows From DataFrame

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 29, 2023
Spread the love

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.

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 a quick example
# 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 results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


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)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      2500
r4   pandas  24000   60days      2000

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 wanted 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(df2)

Yields below output.


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

3. Delete 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 with out creating copy.

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

Yields 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, 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 same output as above.

5. Complete Example For Drop First N Rows From DataFrame

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

Leave a Reply

You are currently viewing Pandas Drop First N Rows From DataFrame