Delete column from pandas DataFrame

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

    We can Remove or Delete a specified column or sprcified columns by drop() method.

    Suppose df is a dataframe.

    Column to be removed = column0

    Code:

    df = df.drop(column0, axis=1)
    

    To remove multiple columns col1, col2, . . . , coln, we have to insert all the columns that needed to be removed in a list. Then remove them by drop() method.

    Code:

    df = df.drop([col1, col2, . . . , coln], axis=1)
    

    I hope it would be helpful.

提交回复
热议问题