Reputation: 367
I'm working with a Dataframe containing some columns with the brand of the product, a few questions from a questionnaire and a few answers. I want to filter only the columns containing the questions and the brand. This is what I've tried:
df.filter(regex=('brand','question'))
I tried to change a few things after regex but nothing worked. Is there anything I can do? I was hoping to use this sort of code, because what I am actually doing is filtering the columns after filtering the ids. So this is what my code looks like:
df[df['id'].isin(id_sample)].filter(regex=('brand','question'))
Thanks in advance!
Upvotes: 1
Views: 46
Reputation: 34086
Something like this would work:
df.filter(regex='brand|question')
OR
you can use a list comprehension
as well:
cols = [col for col in df.columns if 'brand' in col or 'question' in col]
df = df[cols]
Upvotes: 1