Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str.startswith using Regex

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
like image 624
K. ossama Avatar asked Aug 24 '16 16:08

K. ossama


2 Answers

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]).

like image 187
Mad Physicist Avatar answered Sep 29 '22 20:09

Mad Physicist


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
like image 43
A. Garcia-Raboso Avatar answered Sep 29 '22 21:09

A. Garcia-Raboso