Home > Software design >  Find row with exact matching with the string i wanted using CONTAINS, Pandas
Find row with exact matching with the string i wanted using CONTAINS, Pandas

Time:09-19

I have a dataframe below, I wanted to find all name contain 'And'

df = pd.DataFrame({"name": ["Andrew", "Jen And Jess"," Gin And]})

my code
df[df["name"].str.contains('AND',na=False)]

My code's output included with substring and consist 'And'

What i expecting:

name
Jen And Jess
Gin And

CodePudding user response:

To catch upper and lower, you can do:

import re
df[df["name"].str.contains(r'\b[Aa][Nn][Dd]\b')]

CodePudding user response:

use regex in the contain and surround the 'And' with the \b (word boundaries)

df[df.name.str.contains(r'\bAnd\b')]

name
1   Jen And Jess
2   Gin And
  • Related