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 as pd
data = pd.DataFrame({ 'id' : [ 1 , 2 , 3 , 4 ],
'name' : [ 'sai' , 'navya' , 'reema' , 'thanuja' ],
'age' : [ 21 , 22 , 21 , 22 ]})
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 as pd
data = pd.DataFrame({ 'id' : [ 1 , 2 , 3 , 4 ],
'name' : [ 'sai' , 'navya' , 'reema' , 'thanuja' ],
'age' : [ 21 , 22 , 21 , 22 ]})
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 as pd
data = pd.DataFrame({ 'id' : [ 1 , 2 , 3 , 4 ],
'name' : [ 'sai' , 'navya' , 'reema' , 'thanuja' ],
'age' : [ 21 , 22 , 21 , 22 ]})
data.tail(data.shape[ 0 ] - 1 )
|
Output:
Last Updated :
28 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...