Delete the first three rows of a dataframe in pandas


Question

I need to delete the first three rows of a dataframe in pandas.

I know df.ix[:-1] would remove the last row, but I can't figure out how to remove first n rows.

1
130
3/6/2019 9:48:53 PM

Accepted Answer

Use iloc:

df = df.iloc[3:]

will give you a new df without the first three rows.

181
10/17/2018 1:06:06 AM

I think a more explicit way of doing this is to use drop.

The syntax is:

df.drop(label)

And as pointed out by @tim and @ChaimG, this can be done in-place:

df.drop(label, inplace=True)

One way of implementing this could be:

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

And another "in place" use:

df.drop(df.head(3).index, inplace=True)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon