Al_Iskander Al_Iskander - 9 months ago 34
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?

df.drop([col for col in df.columns if 'chair' in col],axis=1,inplace=True)