Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete the first three rows of a dataframe in pandas

Tags:

python

pandas

People also ask

How do you delete the first 5 rows in pandas?

Using iloc[] to Drop First N Rows of DataFrameUse DataFrame. iloc[] the indexing syntax [n:] with n as an integer to select the first n rows from pandas DataFrame. For example df. iloc[n:] , substitute n with the integer number specifying how many rows you wanted to delete.

How do I delete first 10 rows in pandas?

Use drop() to remove first N rows of pandas dataframe To make sure that it removes the rows only, use argument axis=0 and to make changes in place i.e. in calling dataframe object, pass argument inplace=True.

How do you delete the first few rows in python?

In this article, we will discuss different ways to delete first row of a pandas dataframe in python. Use iloc to drop first row of pandas dataframe. Use drop() to remove first row of pandas dataframe. Use tail() function to remove first row of pandas dataframe.

How do I delete the last 3 rows in a Dataframe?

We can remove the last n rows using the drop() method. drop() method gets an inplace argument which takes a boolean value. If inplace attribute is set to True then the dataframe gets updated with the new value of dataframe (dataframe with last n rows removed).


Use iloc:

df = df.iloc[3:]

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


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)

df = df.iloc[n:]

n drops the first n rows.


A simple way is to use tail(-n) to remove the first n rows

df=df.tail(-3)


df.drop(df.index[[0,2]])

Pandas uses zero based numbering, so 0 is the first row, 1 is the second row and 2 is the third row.