英文:
how to search for caret (^) in string
问题
我有一个带有许多字符串的pandas数据帧。其中一些字符串包含插入符号(即^符号)。
我试图使用以下方法删除它们:
df['text'] = df[df['text'].str.contains('^') == False]
我没有收到错误消息,但它在每一行中都找到了插入符号,这是不正确的。这个符号有什么特殊之处吗?
英文:
I have a pandas dataframe with a bunch of strings. Some of the strings contain a caret (ie. a ^ symbol).
I am trying to remove them using this:
df['text'] = df[df['text'].str.contains('^') == False]
I don't get an error but it is finding a caret in every row which is not correct. Is there something special about that symbol?
答案1
得分: 1
根据评论,您必须转义插入符号(caret)或禁用默认的正则表达式处理:
>>> import pandas as pd
>>> df = pd.DataFrame({'text':['abc','d^e','fgh']})
>>> df
text
0 abc
1 d^e
2 fgh
>>> df[df.text.str.contains('^', regex=False) == False]
text
0 abc
2 fgh
>>> df[df.text.str.contains('\^') == False]
text
0 abc
2 fgh
请注意,虽然 df.text.str.contains('\^') == False
有效,但惯例上使用 ~
来反转布尔值:
df[~df.text.str.contains('\^')]
英文:
Per the comments, you must escape the caret or disable the default regex processing:
>>> import pandas as pd
>>> df = pd.DataFrame({'text':['abc','d^e','fgh']})
>>> df
text
0 abc
1 d^e
2 fgh
>>> df[df.text.str.contains('^', regex=False) == False]
text
0 abc
2 fgh
>>> df[df.text.str.contains('\^') == False]
text
0 abc
2 fgh
Note, while df.text.str.contains('\^') == False
works, it's customary to invert the Boolean with ~
.
df[~df.text.str.contains('\^')]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论