0


I want to identify "how" from python strings.

string1
how to find
it is a show
okay how to


Used:

df[df['string1'].str.contains("how",case=False)]

Output coming:

string1
how to find
it is a show
okay how to

As 'show' contains 'how'

output needed:

string1
how to find
okay how to

after that i used

df[df['string1'].str.contains(r"\Whow",case=False)]

but output i got is:

string1
okay how to

which is again worng.
regex '\W' is not including nothing.
how to achieve this?

lil-wolf
  • 372
  • 2
  • 15

2 Answers2

2

You need boundaries (\b) for the pattern, otherwise it will also match substrings contained in words:

df[df['string1'].str.contains(r"\bhow\b",case=False)]

      string1
1    how to find
3    okay how to
dtype: object
yatu
  • 86,083
  • 12
  • 84
  • 139
2

Make it as word boundary using \b using regex https://regex101.com/r/2Dlnxj/1

df['string1'].str.contains("\bhow\b",case=False)
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103