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:
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With