【问题标题】:pandas str.contains() gives wrong results?pandas str.contains() 给出错误的结果?
【发布时间】:2019-02-06 03:41:45
【问题描述】:

例如;

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”。传递的字符串中的序列。 '点'字符没有任何意义?

【问题讨论】:

    标签: python string pandas


    【解决方案1】:

    正则表达式. 表示匹配任何字符。解决办法是转义.或者加参数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
    

    【讨论】:

    • 谢谢。我确实尝试过“SK\”。首先,但没有得到这个想法。现在我注意到默认参数是 ** regex=True **。现在可以了。谢谢。
    猜你喜欢
    • 2018-08-24
    • 2014-09-16
    • 2011-11-15
    • 2019-02-06
    • 2014-01-30
    • 2018-08-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多