统计每个类别的列内容,有一组类别
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