字符串精确匹配 str.count(),pandas python

编程入门 行业动态 更新时间:2024-10-09 21:27:43
本文介绍了字符串精确匹配 str.count(),pandas python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

统计每个类别的列内容,有一组类别

Counting the column contents for each category, have a set of categories

  • 雄猫
  • 母猫,
  • 公-猫-母
  • 使用df.temp.str.count(i),它显示雄猫计数为'2',当搜索只有'1',错误地包含了第三类的子串.

    With df.temp.str.count(i), it is showing Male-Cat count as '2', which when search in data that is only '1', wrongly including substring of third category.

    任何帮助将不胜感激.

    推荐答案

    您需要使用来自此处的正则表达式:>

    You need use regex from here:

    df['count'] = df.temp.str.count(r'(?<!\S)Male-Cat(?!\S)') print (df) temp count 0 Male-Cat Female-Cat Male-Cat-Female Male-Cat 2 1 Male-Cat-Female 0 2 Male-Cat 1 3 Male-Cat Male-Cat Male-Cat-Female 2 4 Male-Cat Male-Cat Male-Cat 3

    更多推荐

    字符串精确匹配 str.count(),pandas python

    本文发布于:2023-11-30 07:49:56,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1649238.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:字符串   精确   str   python   pandas

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!