Drop first row of pandas dataframe (3 Ways)

How to delete first row of pandas dataframe in Python ?

In this article we will discuss about different ways to delete first row of pandas dataframe in Python.

Method-1 : By using iloc attribute :

An iloc attribute is there in Pandas by using which we can select a portion of the dataframe that may be few columns or rows which is simply called as position based indexing.

Syntax - df.iloc[start_row:end_row , start_column, end_column]

Where,

  • start_row : It refers to the row position from where it will start selection.(Default value is 0)
  • end_row : It refers to the row position from where it will end selection.(Default value is upto last row of the dataframe)
  • start_column : It refers to the column position from where it will start selection.(Default value is 0)
  • end_column : It refers to the column position from where it will end selection.(Default value is upto last column of the dataframe)

So, we can select all the rows of the dataframe except the first row and assign back the selected rows to the original variable which will give an effect that the first row has been deleted from the dataframe.

To achieve this, select dataframe from row-2 and select all columns. Row-2 means we will select from index position-1 (as index position starts from 0 in dataframe) upto last row. And to select all columns use default values i.e (:)

i.e

df = df.iloc[1: , :]

So let’s see the implementation of it.

# Program :

import pandas as pd
# List of tuples created
empoyees = [('A',1,'a',10),
            ('B',2,'b',20),
            ('C',3,'c',30) ,
            ('D',4,'d',40)]
# DataFrame object created
df = pd.DataFrame(  empoyees, columns=['Upper', 'Smaller', 'Lower', 'Bigger'])
print("Contents of the original Dataframe : ")
print(df)
# Dropping first row 
df = df.iloc[1: , :]
print("Contents of modified Dataframe : ")
print(df)
Output :
Contents of the original Dataframe : 
  Upper  Smaller  Lower Bigger
0   A         1          a           10
1   B         2          b           20
2   C         3          c           30
3   D        4           d          40
Contents of modified Dataframe : 
    Upper Smaller Lower Bigger
1     B       2            b        20
2     C       3            c        30
3     D       4            d        40

Method-2 : Using drop() function :

There is an drop() function in Panda’s dataframe which can be used to delete any rows from the dataframe.  To make sure that rows only will be deleted then select axis=0 and pass argument inplace=True.

So let’s see the implementation of it.

# Program :

import pandas as pd
# List of tuples created
empoyees = [('A',1,'a',10),
            ('B',2,'b',20),
            ('C',3,'c',30) ,
            ('D',4,'d',40)]
# DataFrame object created
df = pd.DataFrame(  empoyees, columns=['Upper', 'Smaller', 'Lower', 'Bigger'])
print("Contents of the original Dataframe : ")
print(df)
# Dropping first row 
df.drop(index=df.index[0], 
        axis=0, 
        inplace=True)
        
print("Contents of modified Dataframe : ")
print(df)
Output : 
Contents of the original Dataframe : 
    Upper Smaller Lower Bigger
0     A           1         a       10
1     B           2         b       20
2     C           3         c       30
3     D           4         d      40
Contents of modified Dataframe : 
    Upper Smaller Lower Bigger
1     B         2           b       20
2     C         3           c       30
3     D        4            d       40

Method-3 : Using tail() function :

In python, dataframe provides a tail(n) function which returns last ‘n’ rows. So to select all the rows except first row we can pass tail(n-1) which means first row deleted. And it assign back the selected rows to the original variable.

So let’s see the implementation of it.

# Program :

import pandas as pd
# List of tuples created
empoyees = [('A',1,'a',10),
            ('B',2,'b',20),
            ('C',3,'c',30) ,
            ('D',4,'d',40)]
# DataFrame object created
df = pd.DataFrame(  empoyees, columns=['Upper', 'Smaller', 'Lower', 'Bigger'])
print("Contents of the original Dataframe : ")
print(df)

# Deleting first row by selecting last n-1 rows
df = df.tail(df.shape[0] -1)
        
print("Contents of modified Dataframe : ")
print(df)
Output :
Contents of the original Dataframe : 
     Upper Smaller Lower Bigger
0      A         1          a         10
1      B         2          b         20
2      C         3          c         30
3      D         4          d        40
Contents of modified Dataframe : 
    Upper Smaller Lower Bigger
1     B         2          b       20
2     C         3          c       30
3     D         4          d       40