pandas str.contains 列表(list) 模糊匹配

使用pandas时,str.contains()进行一次模糊匹配多个值

方法1 :

search = ['python','java','go']
found = [s['language].str.contains(x) for x in searchfor]

 

 

方法二:

search = ['python','java','go']
found = [s['language'].str.contains('|').join(search)]

目前遇到的问题:

问题1:

TypeError: sequence item 0: expected string, int found

原因:列表中的字段有包含数字,注意检查是否包含数字

解决办法:

将数字转为字符串

search = ['python','java','go']
found = [s['language'].str.contains('|'.join(str(v) for v in search))]

问题二:

error: missing ), unterminated subpattern at position 2

原因:字段中有(),()需要转义

 

 

 

    原文作者:木土a
    原文地址: https://blog.csdn.net/weixin_41018467/article/details/89159443
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞