`error: unbalanced parenthesis` while checking if an item presents in a pandas dataframe

ⅰ亾dé卋堺 提交于 2019-12-04 07:12:50

You can set regex=False in in pandas.Series.str.contains:

df.B.str.contains("six)", regex=False).any()

If you want to match irrespective of case,

df.B.str.contains("Six)", case=False, regex=False).any() 
out[]: True

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.contains.html

Info:

Parenthesis are special characters in regular expressions that need to be "escaped", see for example here or here.

You need escape ) by \ because special regex character:

df.B.str.contains("six\)").any()

More general:

import re

df.B.str.contains(re.escape("six)")).any()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!