Drop columns whose name contains a specific string from pandas DataFrame

后端 未结 11 1966
臣服心动
臣服心动 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:11

    You can filter out the columns you DO want using 'filter'

    import pandas as pd
    import numpy as np
    
    data2 = [{'test2': 1, 'result1': 2}, {'test': 5, 'result34': 10, 'c': 20}]
    
    df = pd.DataFrame(data2)
    
    df
    
        c   result1     result34    test    test2
    0   NaN     2.0     NaN     NaN     1.0
    1   20.0    NaN     10.0    5.0     NaN
    

    Now filter

    df.filter(like='result',axis=1)
    

    Get..

       result1  result34
    0   2.0     NaN
    1   NaN     10.0
    

提交回复
热议问题