Learn Before
Concept
Pandas .drop()
.drop() is used to drop rows/index or columns of a dataframe by their label.
- To specify whether you would like to drop rows/index or columns, you can use the axis parameter within the drop function where axis = 0 is to drop from the rows/index and axis = 1 is to drop columns.
- An alternative to this is using the index or columns parameters to explicitly say where you would like to drop from.
- To change the dataframe permanently, you can set the inplace parameter to true or else it will return a copy of the dataframe.
Given the pictured dataframe (df), if you would like to drop columns “B” and “C” you can run…
#Using axis parameter df.drop(['B', 'C'], axis=1) #Using columns parameter df.drop(columns=['B', 'C'])
Given the pictured dataframe (df), if you would like to drop the rows with indices 0 and 1 you can run…
#Using axis parameter df.drop([0, 1], axis=0) #Using index parameter df.drop(index=[0,1])

0
1
Updated 2021-05-25
Tags
Python Programming Language
Data Science