TheDarkKnight
TheDarkKnight

Reputation: 421

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?

Upvotes: 4

Views: 4087

Answers (1)

jezrael
jezrael

Reputation: 862511

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

Upvotes: 12

Related Questions