【发布时间】:2019-05-06 10:05:38
【问题描述】:
我正在尝试使用 pandas str.contains() 函数和包含如下所示变量的正则表达式从数据框中选择行。
df = pd.DataFrame(["A test Case","Another Testing Case"], columns=list("A"))
variable = "test"
df[df["A"].str.contains(r'\b' + variable + '\b', regex=True, case=False)] #Returns nothing
虽然上面没有返回任何内容,但下面会按预期返回相应的行
df[df["A"].str.contains(r'\btest\b', regex=True, case=False)] #Returns values as expected
任何帮助将不胜感激。
【问题讨论】:
-
也许您的问题是将原始字符串连接到标准字符串?也许试试
fr'\b{variable}\b'
标签: python regex pandas contains