简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM