How to drop rows in a pandas dataframe

1

A previous tutorial showed you how to drop columns in a pandas dataframe. Now we will look at how to drop rows in a pandas dataframe.

There are multiple methods for how to drop rows in pandas dataframe.

We will once again work with Titanic data. Dropping rows in pandas are full of options. This doesn’t mean we will cover all of them, so if you have a question leave it below.

See the full code in our gist or skip to the end of this article.

The most basic way to drop rows in pandas is with axis=0

titanic.drop([0], axis=0)

drop rows in pandas

Here you drop two rows at the same time using pandas

titanic.drop([1, 2], axis=0)

drop rows in pandas

Super simple approach to drop a single row in pandas

titanic.drop([3])

drop rows in pandas

Drop specific items within a column in pandas

Here we will drop male from the sex column.

titanic[titanic.sex != 'male']

drop rows in pandas

Drop multiple rows in pandas

This is how to drop a range of rows within pandas.

titanic.drop(titanic.index[[2,3]])

Enjoy the full code!

https://gist.github.com/craine/df0a79c5b16438dbfd5b538210bd8d0c

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here