Skip to main content
 首页 » 编程设计

python之pandas str.contains() 给出了错误的结果

2025年05月04日14三少

例如;

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 

为什么第三个输出为真?没有'SK'。传递的字符串中的序列。 '点' 字符没有任何意义?

请您参考如下方法:

Regex . 表示匹配任意字符。解决方案是转义.或者添加参数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