Issue
Can i understand why the str.startswith() is not dealing with Regex :
col1
0 country
1 Country
i.e : df.col1.str.startswith('(C|c)ountry')
it returns all the values False :
col1
0 False
1 False
Solution
Series.str.startswith
does not accept regex because it is intended to behave similarly to str.startswith
in vanilla Python, which does not accept regex. The alternative is to use a regex match (as explained in the docs):
df.col1.str.contains('^[Cc]ountry')
The character class [Cc]
is probably a better way to match C
or c
than (C|c)
, unless of course you need to capture which letter is used. In this case you can do ([Cc])
.
Answered By - Mad Physicist
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.