'Using a variable within str.contains()

Pretty much the title. Any way to use a variable to filter in str.contain()? i have been unsuccessful in using a str+@variable



Solution 1:[1]

Try this:

var = 'test'
df = df[df['test_col'].str.contains(var)]

This should work!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 William Rosenbaum