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

The*_*ght 1 python string pandas

例如;

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
Run Code Online (Sandbox Code Playgroud)

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

jez*_*ael 7

正则表达式.意味着匹配任何字符.解决方案是转义.或添加参数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
Run Code Online (Sandbox Code Playgroud)