Al_Iskander Al_Iskander - 4 months ago 8
Python Question

drop column based on a string condition

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

Example:

house1 house2 chair1 chair2
index
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?
Thanks.

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