简体   繁体   English

从列名包含特定字符串的数据框中创建列表

[英]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.

相关问题 从数据框的两列创建一个新列,其中每列的行包含字符串格式的列表 - Create a new column from two columns of a dataframe where rows of each column contains list in string format 从名称包含特定字符串的列创建新列 - creating new column from columns whose name contains a specific string 如何确定 dataframe 列是否包含特定列表,与其顺序无关? - How to determine if a dataframe column contains a particular list, independently of its order? 在检查列值是否包含作为列表中元素的字符串后,如何将列表中的元素分配给数据框列? (Python) - How to assign element from a list to a dataframe column after checking if a column value contains a string that is an element in the list? (Python) 如何过滤列包含存储在列表中的值的 DataFrame? - How to Filter a DataFrame Where a Column Contains Values Stored in a List? 从 pandas DataFrame 中删除名称包含特定字符串的第一个(或任何第 n 个)列 - Drop the first (or any nth) column whose name contains a specific string from pandas DataFrame 如果数据框以列表中的字符串开头,则按列名拆分数据框 - split dataframe by column name if it starts with string in list 删除pandas DataFrame中的行,其中行包含列表中的字符串? - Removing rows in a pandas DataFrame where the row contains a string present in a list? 如何从列表创建单个列的DataFrame,其中第一个元素是python中的列名 - How to create a DataFrame of a single column from a list where the first element is the column name in python 如何在整个Pandas数据帧中搜索字符串,并获取包含该字符串的列的名称? - How to search entire Pandas dataframe for a string and get the name of the column that contains it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM