Exact string match str.count(), pandas python

前端 未结 1 843
小蘑菇
小蘑菇 2021-01-25 10:48

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

  1. Male-Cat,
  2. Female-Cat,
  3. Male-Cat-Female

With df.temp.

相关标签:
1条回答
  • 2021-01-25 11:15

    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
    
    0 讨论(0)
提交回复
热议问题