Find web or mobile developer by Re function

I try to return how many times that web developer and mobile developer show in the columns. I use the | to indicate or in the pattern, and also ignore the case. But the result is incorrect.

Screen Link: https://app.dataquest.io/m/310/guided-project%3A-finding-the-best-markets-to-advertise-in/3/checking-for-sample-representativity

My Code:

import re 
pattern='web developer|mobile develop'
web_or_mob=interest_nonull.str.contains(pattern,re.IGNORECASE)
web_or_mob.value_counts()

What I expected to happen:
True 6030
False 962

What actually happened:

True     6991
False     1

I recommend testing your regex in this great tool: RegEx101…

Hi @candiceliu93,

You forgot to assign it as an argument to the parameter flags:

web_or_mob=interest_nonull.str.contains(pattern, flags=re.IGNORECASE)

oh yes! Flag! Thanks!! BTW, is the pattern correct? I can use | in the pattern, right? also,do i need to use r in fort of 'web developer | mobile develop'?

Yes, sure, you can use | in the pattern. By the way, probably it was a typo in your pattern, but use better “mobile developer”, it seems this word is cut in your pattern (even though, of course, “mobile develop” will also find a string with “mobile developer” :slightly_smiling_face:).
'web developer|mobile developer'.

It works perfectly also without using r in front, look:

import re
example = pd.Series(['Web developer', 'mobile developer', 'mobile Developer',
               'Mobile Developer', 'teacher', 'writer', 'Web devel'])
pattern = 'web developer|mobile developer'
web_or_mob=example.str.contains(pattern, flags=re.IGNORECASE)
web_or_mob

Output:

0     True
1     True
2     True
3     True
4    False
5    False
6    False
dtype: bool

Please pay attention: don’t add any redundant white spaces in your pattern between the words and |. I mean, this is correct:
'web developer|mobile developer'.
And this is wrong:
'web developer | mobile developer'.

1 Like

Thank you!!! love your answer!! so so so detailed. Helpful!!

1 Like

That’s great @candiceliu93, I’m happy it was helpful! :star_struck: