Delete column from pandas DataFrame

后端 未结 17 1379
一生所求
一生所求 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:21

    Pandas 0.21+ answer

    Pandas version 0.21 has changed the drop method slightly to include both the index and columns parameters to match the signature of the rename and reindex methods.

    df.drop(columns=['column_a', 'column_c'])
    

    Personally, I prefer using the axis parameter to denote columns or index because it is the predominant keyword parameter used in nearly all pandas methods. But, now you have some added choices in version 0.21.

提交回复
热议问题