Drop columns whose name contains a specific string from pandas DataFrame

后端 未结 11 1962
臣服心动
臣服心动 2020-11-29 16:19

I have a pandas dataframe with the following column names:

Result1, Test1, Result2, Test2, Result3, Test3, etc...

I want to drop all the columns whose name c

11条回答
  •  醉话见心
    2020-11-29 17:07

    Cheaper, Faster, and Idiomatic: str.contains

    In recent versions of pandas, you can use string methods on the index and columns. Here, str.startswith seems like a good fit.

    To remove all columns starting with a given substring:

    df.columns.str.startswith('Test')
    # array([ True, False, False, False])
    
    df.loc[:,~df.columns.str.startswith('Test')]
    
      toto test2 riri
    0    x     x    x
    1    x     x    x
    

    For case-insensitive matching, you can use regex-based matching with str.contains with an SOL anchor:

    df.columns.str.contains('^test', case=False)
    # array([ True, False,  True, False])
    
    df.loc[:,~df.columns.str.contains('^test', case=False)] 
    
      toto riri
    0    x    x
    1    x    x
    

    if mixed-types is a possibility, specify na=False as well.

提交回复
热议问题