0 votes
1 view
in Machine Learning by (33.2k 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.2k 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 (38.2k points)

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

df = df.iloc[3:]

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...