To delete a column in a DataFrame, I can successfully use:
But why can’t I use the following?
Since it is possible to access the column/Series as
df.column_name, I expected this to work.
As you’ve guessed, the right syntax is
It’s difficult to make
del df.column_name work simply as the result of syntactic limitations in Python.
del df[name] gets translated to
df.__delitem__(name) under the covers by Python.
columns = ['Col1', 'Col2', ...] df.drop(columns, inplace=True, axis=1)
This will delete one or more columns in-place. Note that
inplace=True was added in pandas v0.13 and won’t work on older versions. You’d have to assign the result back in that case:
df = df.drop(columns, axis=1)