Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 using the following: enter image description here

and then tried a series of ways to drop that group that resulted in a variety of errors. Can anyone please, propose a way to remove these columns?

like image 210
Tanya Avatar asked Jul 17 '16 21:07

Tanya


1 Answers

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
like image 83
MaxU - stop WAR against UA Avatar answered Sep 28 '22 12:09

MaxU - stop WAR against UA