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
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])
.
Series.str.startswith
does not accept regexes. Use Series.str.match
instead:
df.col1.str.match(r'(C|c)ountry', as_indexer=True)
Output:
0 True
1 True
Name: col1, dtype: bool
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With