Al_Iskander Al_Iskander - 1 year ago 130
Python Question

drop column based on a string condition

How can I delete a dataframe column based on a certain string in its name?


house1 house2 chair1 chair2
1 foo lee sam han
2 fowler smith had sid
3 cle meg mag mog

I want to drop the columns that contain 'chair' in the string.
How can this be done in an efficient way?

Answer Source
df.drop([col for col in df.columns if 'chair' in col],axis=1,inplace=True)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download