反转string.contains在python,pandas中 [英] Reversal of string.contains In python, pandas
问题描述
我的代码中有类似的内容:
I have something like this in my code:
df2 = df[df['A'].str.contains("Hello|World")]
但是,我希望所有不的行都包含Hello或World.如何最有效地扭转这种情况?
However, I want all the rows that don't contain either of Hello or World. How do I most efficiently reverse this?
推荐答案
您可以使用波浪号~
来反转布尔值:
You can use the tilde ~
to flip the bool values:
>>> df = pd.DataFrame({"A": ["Hello", "this", "World", "apple"]})
>>> df.A.str.contains("Hello|World")
0 True
1 False
2 True
3 False
Name: A, dtype: bool
>>> ~df.A.str.contains("Hello|World")
0 False
1 True
2 False
3 True
Name: A, dtype: bool
>>> df[~df.A.str.contains("Hello|World")]
A
1 this
3 apple
[2 rows x 1 columns]
这不是最有效的方法吗?您必须将其与其他选项相对应.有时使用正则表达式比df[~(df.A.str.contains("Hello") | (df.A.str.contains("World")))]
之类的东西要慢,但是我很难猜测交叉点在哪里.
Whether this is the most efficient way, I don't know; you'd have to time it against your other options. Sometimes using a regular expression is slower than things like df[~(df.A.str.contains("Hello") | (df.A.str.contains("World")))]
, but I'm bad at guessing where the crossovers are.
这篇关于反转string.contains在python,pandas中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!