1

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.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.

Any help will be appreciated.

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
Nayeem
  • 91
  • 14
  • 1
    I think you should post some data, along with a better description of what you want. – cs95 Oct 16 '17 at 07:53

1 Answers1

4

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252