Wednesday, 30 August 2017

python - Delete column from pandas DataFrame



When deleting a column in a DataFrame I use:



del df['column_name']



And this works great. Why can't I use the following?



del df.column_name


As you can access the column/Series as df.column_name, I expect this to work.


Answer



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.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...