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)
Here you drop two rows at the same time using pandas
titanic.drop([1, 2], axis=0)
Super simple approach to drop a single row in pandas
titanic.drop([3])
Drop specific items within a column in pandas
Here we will drop male from the sex column.
titanic[titanic.sex != 'male']
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
[…] allow for many methods for adding and dropping content. We have covered how to drop a column and how to drop a row in pandas dataframe. What if you want to add a column to pandas? You can handle that in a few simple […]