I'm attempting to select rows from a dataframe using the pandas str.contains()
function with a regular expression that contains a variable as shown below.
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
While the above returns nothing, the following returns the appropriate row as expected
df[df["A"].str.contains(r'\btest\b', regex=True, case=False)] #Returns values as expected
Any help would be appreciated.
fr'\b{variable}\b'
– Uglify