查找名称包含特定字符串的列 [英] Find column whose name contains a specific string
问题描述
我有一个带有列名的数据框,我想找到一个包含特定字符串但与之不完全匹配的数据框.我正在像'spike-2'
,'hey spike'
,'spiked-in'
这样的列名中搜索'spike'
('spike'
部分始终是连续的).
I have a dataframe with column names, and I want to find the one that contains a certain string, but does not exactly match it. I'm searching for 'spike'
in column names like 'spike-2'
, 'hey spike'
, 'spiked-in'
(the 'spike'
part is always continuous).
我希望列名以字符串或变量的形式返回,因此我以后可以正常使用df['name']
或df[name]
来访问列.我试图找到方法来做到这一点,无济于事.有提示吗?
I want the column name to be returned as a string or a variable, so I access the column later with df['name']
or df[name]
as normal. I've tried to find ways to do this, to no avail. Any tips?
推荐答案
仅遍历DataFrame.columns
,现在这是一个示例,在该示例中,您将最终获得匹配的列名列表:
Just iterate over DataFrame.columns
, now this is an example in which you will end up with a list of column names that match:
import pandas as pd
data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]}
df = pd.DataFrame(data)
spike_cols = [col for col in df.columns if 'spike' in col]
print(list(df.columns))
print(spike_cols)
输出:
['hey spke', 'no', 'spike-2', 'spiked-in']
['spike-2', 'spiked-in']
说明:
-
df.columns
返回列名列表 -
[col for col in df.columns if 'spike' in col]
使用变量col
遍历列表df.columns
,如果col
包含'spike'
,则将其添加到结果列表中.此语法为列表理解.
df.columns
returns a list of column names[col for col in df.columns if 'spike' in col]
iterates over the listdf.columns
with the variablecol
and adds it to the resulting list ifcol
contains'spike'
. This syntax is list comprehension.
如果只希望结果数据集具有匹配的列,则可以执行以下操作:
If you only want the resulting data set with the columns that match you can do this:
df2 = df.filter(regex='spike')
print(df2)
输出:
spike-2 spiked-in
0 1 7
1 2 8
2 3 9
这篇关于查找名称包含特定字符串的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!