如果我有一个熊猫数据帧,看起来像这样:
Sequence Rating
0 HYHIVQKF 1
1 YGEIFEKF 2
2 TYGGSWKF 3
3 YLESFYKF 4
4 YYNTAVKL 5
5 WPDVIHSF 6
这是我正在使用的代码,返回与以下模式匹配的行:
\b.[YF]\w+[LFI]\b
pat = r'\b.[YF]\w+[LFI]\b'
new_df.Sequence.str.contains(pat)
new_df[new_df.Sequence.str.contains(pat)]
上面的代码返回与模式匹配的行,但是我可以使用什么来返回不匹配的行呢?你知道吗
预期产量:
Sequence Rating
1 YGEIFEKF 2
3 YLESFYKF 4
5 WPDVIHSF 6
可以对not使用
~
:只需对现有的布尔级数求反即可:
这将为您提供所需的输出:
简要说明:
将返回布尔级数:
用
~
取反得到这是另一个可以传递到原始数据帧的布尔序列。你知道吗
Psidom's answer更为优雅,但解决此问题的另一种方法是修改regex模式以使用否定的先行断言,然后使用
match()
而不是contains()
:相关问题 更多 >
编程相关推荐