Delete column from pandas DataFrame

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

    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.

    0 讨论(0)
  • 2020-11-22 03:27

    Deleting a column using iloc function of dataframe and slicing, when we have a typical column name with unwanted values.

    df = df.iloc[:,1:] # removing an unnamed index column
    

    Here 0 is the default row and 1 is 1st column so ,1 where starts and stepping is taking default values, hence :,1: is our parameter for deleting the first column.

    0 讨论(0)
  • 2020-11-22 03:28

    The dot syntax works in JavaScript, but not in Python.

    • Python: del df['column_name']
    • JavaScript: del df['column_name'] or del df.column_name
    0 讨论(0)
  • 2020-11-22 03:32

    Use:

    columns = ['Col1', 'Col2', ...]
    df.drop(columns, inplace=True, axis=1)
    

    This will delete one or more columns in-place. Note that inplace=True was added in pandas v0.13 and won't work on older versions. You'd have to assign the result back in that case:

    df = df.drop(columns, axis=1)
    
    0 讨论(0)
  • 2020-11-22 03:32

    from version 0.16.1 you can do

    df.drop(['column_name'], axis = 1, inplace = True, errors = 'ignore')
    
    0 讨论(0)
  • 2020-11-22 03:32

    In pandas 0.16.1+ you can drop columns only if they exist per the solution posted by @eiTanLaVi. Prior to that version, you can achieve the same result via a conditional list comprehension:

    df.drop([col for col in ['col_name_1','col_name_2',...,'col_name_N'] if col in df], 
            axis=1, inplace=True)
    
    0 讨论(0)
提交回复
热议问题