Explain Codes LogoExplain Codes Logo

Delete the first three rows of a dataframe in pandas

python
dataframe
pandas
data-manipulation
Anton ShumikhinbyAnton Shumikhin·Feb 4, 2025
TLDR

Here's your one-liner to discard the top three rows from your DataFrame df:

df = df.iloc[3:]

In Python speak, we're slicing df to start from the fourth row and beyond.

Go deeper: Alternatives and considerations

Hang on tight! We'll tour through different methods, scenarios, and precautions for the task at hand.

Modifying in place with drop and index

To delete the first three rows directly in the dataframe:

# Bare with me df, this might sting a bit! df.drop(df.index[:3], inplace=True)

Alternatively:

# Bye bye, top three records! df.drop(df.head(3).index, inplace=True)

Both surgically excise the top three records from df without requiring a new dataframe.

Use of tail() with negative indexing

For those loving minimalism, the tail() method serves the purpose:

# Just the tail end, please! df = df.tail(-3)

Negative indexing makes tail() smart enough to leave out the initial three rows.

Direct hit: Remove rows by explicit indexing

When you need to pinpoint the exact rows to extract:

# Farewell first-class tickets! df.drop(df.index[[0, 1, 2]], inplace=True)

This comes handy when the index is non-sequential or explicit row deletion is important for the code readability.

The counting saga: Zero-based indexing and data integrity

In Python's universe, counting starts from zero, not one. Hence, the first 3 rows correspond to indices [0, 1, 2]. Keep this in mind for flawless dataframe manipulation and to maintain data integrity.

Handling different scenarios like a pro

Trick with callable for logical drop

If in-place modification makes you cringe, a callable can ease your anxiety:

df = df.drop(df.index[:3], inplace=False)

This way, you can chain with other operations for larger data processing tasks.

Original indexing preservation

If you're a stickler for the original indexing post-operation:

# Hold my order, please! df.reset_index(drop=True, inplace=True)

Fire off this just after the row removal to reset the scene.

Handling errors: Index out of bounds

Got fewer rows than what you hope to delete? It's an IndexError alert! But no worries, Python's try-except comes to the rescue:

try: # Easy does it... df = df.iloc[3:] except IndexError: # Oops, the party ended before it started! print("Uh oh, fewer than 3 rows in dataframe!")

This way, your code remains unbroken even for smaller data collections.

Multiple manipulations: The power of method chaining

# I like my data shaken, not stirred! 🍸 df = df.iloc[3:][::-1]

This piece of art not only gracefully removes the top trio but also reverses the order of the remaining rows.