How to delete first row of Pandas DataFrame

In this article, you’ll learn how to delete the first row in Pandas. Three methods are given to delete the first row of Pandas DataFrame.

Method 1: Delete the first row using iloc

# Import the Pandas library as pd
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'col_1': [95, 90, 16, 90],
                   'col_2': [90, 28, 38, 48],
                   'col_3': [90, 64, 94, 54]})

# Display the DataFrame
print(df)

# Delete first row of a DataFrame
df = df.iloc[1:]

# Display the Modified DataFrame
print(df)

Output:

   col_1  col_2  col_3
0     65     80     40
1     60     28     64
2     16     38     94
3     60     48     54
   col_1  col_2  col_3
1     60     28     64
2     16     38     94
3     60     48     54

Method 2

# Import the Pandas library as pd
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'col_1': [95, 90, 16, 90],
                   'col_2': [90, 28, 38, 48],
                   'col_3': [90, 64, 94, 54]})

# Display the DataFrame
print(df)

# Delete first row of a DataFrame
df.drop(df.index[0], inplace=True)

# Display the Modified DataFrame
print(df)

Output:

   col_1  col_2  col_3
0     95     90     90
1     90     28     64
2     16     38     94
3     90     48     54
   col_1  col_2  col_3
1     90     28     64
2     16     38     94
3     90     48     54

Method 3

# Import the Pandas library as pd
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'col_1': [95, 90, 16, 90],
                   'col_2': [90, 28, 38, 48],
                   'col_3': [90, 64, 94, 54]})

# Display the DataFrame
print(df)

# Delete first row of a DataFrame
df = df.tail(df.shape[0]- 1)

# Display the Modified DataFrame
print(df)

Output:

   col_1  col_2  col_3
0     95     90     90
1     90     28     64
2     16     38     94
3     90     48     54
   col_1  col_2  col_3
1     90     28     64
2     16     38     94
3     90     48     54

If you want to learn Pandas, I recommend this book and free resource.

Leave a Comment

Your email address will not be published.