The drop() method is used in Python to delete rows/ columns in a Pandas DataFrame. It is used to remove a particular row or column. Under the parameters of the drop() method, mention the column you want to delete with the axis.
We will now see two examples:
Drop columns
Drop rows
Before moving further, we’ve prepared a video tutorial to delete rows/ columns in a Pandas DataFrame:
Drop columns using drop()
The columns are dropped using the column names. The axis is set to 1 since we want to drop a column. The columns axis can also be used for the drop() method to remove the specified column:
1
2
3
4
5
axis='columns'
or
axis=1
Let us see an example to drop columns using the drop() method:
The rows are dropped using the index label set as a parameter of the drop() method. The rows of that particular label are removed. Set the axis to 0 since we want to drop a row. The rows axis i.e. index can also be used for the drop() method to remove the specified row:
1
2
3
4
5
axis='index'
Or
axis=0
Let us see an example of dropping a row using the drop() method. Here, the row with the 2nd index is removed:
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok
No Comments