python for col in col if - 排除特定字符串

python for col in col if - exclude specific string

我正在尝试拆分一些数据帧,其中有很多 split/created 所以我正在尝试使用 for 循环,但不能完全让它做我想做的事.

有一个数据框(我在下面称它为 column_names)只包含一些列名称,我希望它查看这些较小的数据框以排除 2 个特定列.

我正在使用以下内容:

# target and features
target = ['rougher.output.recovery', 'final.output.recovery']
features = [col for col in column_names if ~col.str.contains('recovery')]

目标是像这样将它们输入数据帧:

#dataframes for each step train and test targets
target_train, target_test = train_imp[target].values,test_imp[target].values
features_train, features_test = train_imp[features].values,test_imp[features].values

我试图排除名称中包含 recovery 的列,但我不太正确。

我试过了:

[col for col in train_test if col != ['rougher.output.recovery', 'final.output.recovery']

[col for col in train_test if not 'rougher.output.recovery' or 'final.output.recovery']

但他们实际上并没有排除我想排除的列?

我也尝试了上面的 .contains,但它不起作用,坦率地说,我对 python 很陌生,我不确定还能尝试什么?

提前感谢您的时间和精力!

使用features = [col for col in column_names if 'recovery' not in col.contains('recovery')]

也不能评论(声望不够,前几天刚注册),不好意思再发一个回答。使用以下理解:

features = [col for col in column_names if 'recovery' not in col]

您的两次尝试也可以更正:

[col for col in train_test if col not in ['rougher.output.recovery', 'final.output.recovery']

[col for col in train_test if col != 'rougher.output.recovery' and col != 'final.output.recovery']