Pandas结构包含()给出了错误的结果?

2024-05-15 22:48:56 发布

您现在位置:Python中文网/ 问答频道 /正文

例如:

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.”序列。'dot'字符不代表什么?在


Tags: 字符串falsetrueascdoutseriespd
1条回答
网友
1楼 · 发布于 2024-05-15 22:48:56

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

相关问题 更多 >