Delete column from pandas DataFrame

后端 未结 17 1399
一生所求
一生所求 2020-11-22 02:44

When deleting a column in a DataFrame I use:

del df[\'column_name\']

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

         


        
17条回答
  •  攒了一身酷
    2020-11-22 03:20

    A nice addition is the ability to drop columns only if they exist. This way you can cover more use cases, and it will only drop the existing columns from the labels passed to it:

    Simply add errors='ignore', for example.:

    df.drop(['col_name_1', 'col_name_2', ..., 'col_name_N'], inplace=True, axis=1, errors='ignore')
    
    • This is new from pandas 0.16.1 onward. Documentation is here.

提交回复
热议问题