I have a dataframe with a list of sentences in one column and am trying to create a new column equal to the number of times a list of strings show up.
For example, the relevant dataframe looks like
book['sentences']
0 The brown dog jumped over the big moon
1 The brown fox slid under the brown log
I'm trying to count the number of times "brown", "over", and "log" show up in each sentences (i.e. the new column would be equal to 2 and 3).
I know I can do this with str.count, but only for one string at a time and then I would have to add them up
book['count_brown'] = book['sentences'].str.count('brown')
book['count_over'] = book['sentences'].str.count('over')
book['count_log'] = book['sentences'].str.count('log')
book['count'] = book['count_brown']+book['count_over']+book['count_log']
My list of strings I am searching for is over 300 words long so even with a loop it doesn't seem optimal. Is there a better way to do this?