0

I referred to the solution here - Find column whose name contains a specific string

spike_cols = [col for col in df.columns if 'spike' in col]

However, I want to search for multiple strings at once. For example, I tried to search for string 'keyword' by implementing -

spike_cols = [col for col in df.columns if 'spike | keyword' in col]

This doesn't return the list I desire. Any pointers on how to proceed ?

cottontail
  • 10,268
  • 18
  • 50
  • 51
new2cod3
  • 31
  • 5

1 Answers1

0

Also possible:

# create list for words
words = ['spike', 'keyword']

# store col names in list
cols = list(df.columns)

# list comprehension to return idxs for matches
idx_matches = [i for i in range(len(cols)) if cols[i] in words]

# access the df cols
df.iloc[:, idx_matches]
ouroboros1
  • 9,113
  • 3
  • 7
  • 26