How to remove multiple columns that end with same text in Pandas?

前端 未结 5 1612
一生所求
一生所求 2021-01-18 03:56

I\'m trying to remove a group of columns from a dataset. All of the variables to remove end with the text \"prefix\".

I did manage to \"collect\' them into a group

5条回答
  •  无人共我
    2021-01-18 04:45

    using filter and regex

    df.filter(regex=r'^((?!prefix).)*$')
    

    Demo

    df = pd.DataFrame(np.random.rand(2, 6),
                      columns=['oneprefix', 'one',
                               'twoprefix', 'two',
                               'threeprefix', 'three'])
    
    df.filter(regex=r'^((?!prefix).)*$')
    

    where:

    df
    


    Timing

    All are about the same

提交回复
热议问题