16.3: Removing Rows/Columns
- Page ID
- 39308
Finally, after reading a .csv file into a DataFrame, there are times when you want to manually delete certain rows and/or columns that are not going to be of interest.
The easiest syntax for deleting a row (say, Santa’s Little Helper) is:
Code \(\PageIndex{1}\) (Python):
simpsons = simpsons.drop('SLH')
The .drop() method takes the index of the undesired row as an argument, Like most of the methods we’ve seen so far, it returns a modified copy of the DataFrame it’s called on, so you have to reassign this to the original variable (or use the inplace=True argument).
You can even delete multiple rows at the same time by enclosing the undesired indices in boxies:
Code \(\PageIndex{2}\) (Python):
simpsons = simpsons.drop(['Homer','Marge','SLH'])
Deleting a column is even more common, since many tables “in the wild” have many, many columns, only a few of which you may care about in your analysis. You can whack one entirely with the del operator, just like we did for Serieses (p. 111):
Code \(\PageIndex{3}\) (Python):
del simpsons['IQ']