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

前端 未结 5 1610
一生所求
一生所求 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:39

    for the sake of completeness:

    In [306]: df
    Out[306]:
       prefixcol1  col2prefix  col3prefix  colN
    0           1           1           1     1
    1           2           2           2     2
    2           3           3           3     3
    
    In [307]: df.loc[:, ~df.columns.str.contains('prefix$')]
    Out[307]:
       prefixcol1  colN
    0           1     1
    1           2     2
    2           3     3
    

    or another variant:

    In [388]: df.select(lambda x: re.search(r'prefix$', str(x)) is None, axis=1)
    Out[388]:
       prefixcol1  colN
    0           1     1
    1           2     2
    2           3     3
    

提交回复
热议问题