Delete column from pandas DataFrame

后端 未结 17 1587
一生所求
一生所求 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条回答
  •  Happy的楠姐
    2020-11-22 03:19

    Another way of Deleting a Column in Pandas DataFrame

    if you're not looking for In-Place deletion then you can create a new DataFrame by specifying the columns using DataFrame(...) function as

    my_dict = { 'name' : ['a','b','c','d'], 'age' : [10,20,25,22], 'designation' : ['CEO', 'VP', 'MD', 'CEO']}
    
    df = pd.DataFrame(my_dict)
    

    Create a new DataFrame as

    newdf = pd.DataFrame(df, columns=['name', 'age'])
    

    You get a result as good as what you get with del / drop

提交回复
热议问题