Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to Drop First Row in Pandas?

How to Drop First Row in Pandas?

In this article, we will discuss how to drop the first row in Pandas Dataframe using Python.

Dataset in use:

Method 1: Using iloc() function

Here this function is used to drop the first row by using row index.

Syntax:

df.iloc[row_start:row_end , column_start:column_end]

where,

  • row_start  specifies first row
  • row_end specifies last row
  • column_start specifies first column
  • column_end specifies last column

We can drop the first row by excluding the first row 

Syntax:

data.iloc[1: , :]

Example: Drop the first row

Python3




# import pandas module
import pandas as pd
 
# create student dataframe with 3 columns
# and 4 rows
data = pd.DataFrame({'id': [1, 2, 3, 4],
                     'name': ['sai', 'navya', 'reema', 'thanuja'],
                     'age': [21, 22, 21, 22]})
 
 
# drop first row
data.iloc[1:, :]


Output:

Method 2: Using drop() function

Here we are using the drop() function to remove first row using the index parameter set to 0

Syntax:

data.drop(index=0)

where data is the input dataframe

Example: Drop the first row

Python3




# import pandas module
import pandas as pd
 
# create student dataframe with 3 columns
# and 4 rows
data = pd.DataFrame({'id': [1, 2, 3, 4],
                     'name': ['sai', 'navya', 'reema', 'thanuja'],
                     'age': [21, 22, 21, 22]})
 
 
# drop first row
data.drop(index=0)


Output:

Method 3: Using tail() function

Here tail() is used to remove the last n rows, to remove the first row, we have to use the shape function with -1 index.

Syntax:

data.tail(data.shape[0]-1)

where data is the input dataframe

Example: Drop the first row

Python3




# import pandas module
import pandas as pd
 
# create student dataframe with 3 columns
# and 4 rows
data = pd.DataFrame({'id': [1, 2, 3, 4],
                     'name': ['sai', 'navya', 'reema', 'thanuja'],
                     'age': [21, 22, 21, 22]})
 
 
# drop first row
data.tail(data.shape[0]-1)


Output:

RELATED ARTICLES

Most Popular

Recent Comments