搜索字符串中的插入符 (^) 的方法是:

huangapple go评论67阅读模式
英文:

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('\^')]

huangapple
  • 本文由 发表于 2023年5月17日 08:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定