Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove columns which match some pattern in python

Tags:

python

pandas

I have a large csv files which have several columns as follows:

M_15_19_yr_ M_19_25_yr_ M_25_35_yr_
20 34 12
09 21 19

I want to remove such columns which start from M_{age1}_{age2}_yr. I tried using:

df = df.loc[:, ~df.columns.str.startswith(('M_15_19_yr_','M_19_25_yr_','M_25_35_yr_'))

However, I have many such columns. How do I remove all of such columns without explicitly writing down each column's name?

like image 846
surviving-grad Avatar asked Sep 23 '26 12:09

surviving-grad


1 Answers

You may check with filter

df = df.filter(regex = r'^(?!M_\d+_\d+_yr)')
like image 93
BENY Avatar answered Sep 25 '26 03:09

BENY