这是相关的,但与另一个post不同。
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') & ('hey') in col]这样我就能精确地得到我也用过的一列“嗨斯派克”
dfnew = df.filter(regex='spike'&'hey')获取误差
TypeError:不支持的操作数类型为&:'str‘和'str’
发布于 2017-04-20 15:30:30
下面是一个没有regex的方法,只需使用in检查子字符串条件:
df[[col for col in df.columns if 'hey' in col and 'spike' in col]]

或者,如果您想使用regex,您可以:
df.filter(regex='(?=.*hey)(?=.*spike)')

https://stackoverflow.com/questions/43523742
复制相似问题