0

I have a list of words which i need to match as a whole string in a column named "Description"

For example : Description- Robert is a great R PROGRAMMER!

My Code:

`m=['Refresh','Update','R','Notebook']

The next piece of code is where I need help on:

data['Matched_count']=data.description.str.count('|'.join(m))

The above gives me a count of the substring match. For example - all the 'R' being used in the description is counted

data['Matched_count']=data.description.str.contains('|'.join(m)).astype(int)

The above just gives me a flag of 0 and 1 for the substring match

data['Matched_count']=data.description.str.match('|'.join(m)).astype(int)

This comes close to the entire String matching . However this doesnt give me the count of the string .It just flags it as 1 or 0 based on it being present in the Description

I need to count the occurrence of the entire string in the dataframe in a new column

  • does this helps https://stackoverflow.com/a/36447145/6660373? – Pygirl Jun 12 '20 at 03:36
  • 1
    https://stackoverflow.com/a/46764842/6660373 – Pygirl Jun 12 '20 at 03:41
  • It works with a single key word. But how do i work around with adding a list of words to it? data['Matched_count']=data.description.str.count(r'(?<!\S)'|'.join(m)(?!\S)') doesnt work. and neither does : data['Matched_count']=data.description.str.count(r'(?<!\S)Refresh,R,Notebook(?!\S)') – Gaurav Hegde Jun 12 '20 at 04:06
  • 1
    Does this answer your question? [Exact string match str.count(), pandas python](https://stackoverflow.com/questions/46764572/exact-string-match-str-count-pandas-python) – Rafael Valero Feb 13 '21 at 22:39

0 Answers0