gpt4 book ai didi

python - 从正则表达式模式返回不匹配的行

转载 作者:行者123 更新时间:2023-11-28 20:02:36 24 4
gpt4 key购买 nike

如果我有一个看起来像这样的 pandas 数据框:

      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 :

pat = r'\b.[YF]\w+[LFI]\b'
new_df[~new_df.Sequence.str.contains(pat)]

# Sequence Rating
#1 YGEIFEKF 2
#3 YLESFYKF 4
#5 WPDVIHSF 6

关于python - 从正则表达式模式返回不匹配的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45557050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com