Delete column from pandas DataFrame

后端 未结 17 1695
一生所求
一生所求 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条回答
  •  猫巷女王i
    2020-11-22 03:35

    It's good practice to always use the [] notation. One reason is that attribute notation (df.column_name) does not work for numbered indices:

    In [1]: df = DataFrame([[1, 2, 3], [4, 5, 6]])
    
    In [2]: df[1]
    Out[2]:
    0    2
    1    5
    Name: 1
    
    In [3]: df.1
      File "", line 1
        df.1
           ^
    SyntaxError: invalid syntax
    

提交回复
热议问题