HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonCritical

Delete a column from a Pandas DataFrame

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
pandascolumndataframedeletefrom

Problem

To delete a column in a DataFrame, I can successfully use:
del df['column_name']


But why can't I use the following?
del df.column_name


Since it is possible to access the Series via df.column_name, I expected this to work.

Solution

As you've guessed, the right syntax is

del df['column_name']


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.

Code Snippets

del df['column_name']

Context

Stack Overflow Q#13411544, score: 1337

Revisions (0)

No revisions yet.