代码之家  ›  专栏  ›  技术社区  ›  RustyShackleford

如何按值删除列?

  •  1
  • RustyShackleford  · 技术社区  · 7 年前

    我有一个df,有两个列的名字是一样的。

    数据框:

    type     type
    1        default
    2        default
    

    default ?

    type
    1
    2
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   cs95 abhishek58g    7 年前

    使用 loc 并选择它们:

    df = df.loc[:, ~df.eq('default').any(0)]  # ~df.eq('default').all(0)  # Stricter.
    print (df)
       type
    0     1
    1     2
    

    你不能用 drop , filter

        2
  •  0
  •   Austin A    7 年前

    不如换一种方法,改名列。

    df.columns = ['column_1', 'column_2']
    

    现在,如果我们想删除一个列,我们可以选择特定的列。

    df.drop(columns='column_2', inplace=True)
    
        3
  •  0
  •   btin    7 年前

    index 你想要的那一列,然后打电话给 df.iloc[:,index] 一些文件 here