Consider the following dataframe:
import pandas as pd
df = pd.DataFrame(["What is the answer",
"the answer isn't here, but the answer is 42" ,
"dogs are nice",
"How are you"], columns=['words'])
df
words
0 What is the answer
1 the answer isn't here, but the answer is 42
2 dogs are nice
3 How are you
I want to count the number of appearances of a certain string, that may repeat a few times in each index.
For example, I want to count the number of times the answer
appears.
I tried:
df.words.str.contains(r'the answer').count()
Which I hoped for a solution, but the output is 4
.
Which I don't understand why. the answer
appears 3 times.
What is **the answer**
**the answer** isn't here, but **the answer** is 42
Note: search string may appear more than once in the row