gpt4 book ai didi

python - `error: unbalanced parenthesis` 检查项目是否出现在 pandas 数据框中

转载 作者:太空狗 更新时间:2023-10-30 00:24:46 28 4
gpt4 key购买 nike

df=pd.DataFrame({"A":["one","two","three"],"B":["fopur","give","six"]})

当我这样做的时候,

df.B.str.contains("six").any()
out[2]=True

当我这样做的时候,

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

我收到以下错误,

C:\ProgramData\Anaconda3\lib\sre_parse.py in parse(str, flags, pattern)
868 if source.next is not None:
869 assert source.next == ")"
--> 870 raise source.error("unbalanced parenthesis")
871
872 if flags & SRE_FLAG_DEBUG:

error: unbalanced parenthesis at position 3

请帮忙!

最佳答案

您可以在 in pandas.Series.str.contains 中设置 regex=False:

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

如果想不分大小写地匹配

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

信息:

括号是正则表达式中需要“转义”的特殊字符,参见示例herehere .

关于python - `error: unbalanced parenthesis` 检查项目是否出现在 pandas 数据框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48699907/

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