Deleting columns
Columns can be deleted from a DataFrame
by using the del
keyword or the .pop()
or .drop()
method of the data frame. The behavior of each of these differs slightly:
del
will simply delete theSeries
from theDataFrame
(in-place)pop()
will both delete theSeries
and return theSeries
as a result (also in-place)drop(labels, axis=1)
will return a new data frame with the column(s) removed (the originalDataFrame
object is not modified)
The following demonstrates using del
to delete the BookValue
column from a copy of the sp500
data:

The following uses the .pop()
method to remove the Sector
column:

The .pop()
method has the benefit that it gives us the popped columns.

The .drop()
method can be used to remove both rows and columns. To use it to remove columns, specify axis=1
: