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?
You may check with filter
df = df.filter(regex = r'^(?!M_\d+_\d+_yr)')
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