Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (33.1k points)

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.

2 Answers

0 votes
by (33.1k points)

The latest version of the Pandas library can do this for you easily by using the .dt function.

For example:

df['just_date'] = df['dates'].dt.date

The above code syntax will return a datetime.date dtype, if you want to have a datetime64 and you can easily normalize the time component to midnight so it will set the given values to 00:00:00:

df['normalised_date'] = df['dates'].dt.normalize()

This keeps the dtype as datetime64 but the display shows just the date value.

Hope this answer helps.

0 votes
by (41.4k points)

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

df = df.iloc[3:]

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...