Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas str.contains() gives wrong results?

For example;

pd.Series('ASKING CD.').str.contains('AS')
Out[58]: 
0    True
dtype: bool

pd.Series('ASKING CD.').str.contains('ASG')
Out[59]: 
0    False
dtype: bool

pd.Series('ASKING CD.').str.contains('SK.')
Out[60]: 
0    True
dtype: bool

Why the 3rd output is True? There is no 'SK.' sequence in passed string. 'dot' character doesn't mean anything?

like image 498
TheDarkKnight Avatar asked Dec 17 '22 21:12

TheDarkKnight


1 Answers

Regex . means match any character. Solutions is escape . or add parameter regex=False:

print(pd.Series('ASKING CD.').str.contains(r'SK\.'))
0    False
dtype: bool

print(pd.Series('ASKING CD.').str.contains('SK.', regex=False))
0    False
dtype: bool
like image 94
jezrael Avatar answered Jan 06 '23 11:01

jezrael