[英]creating list from dataframe where column name contains particular string
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 ?
关于如何进行的任何指示?
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.