Reputation: 5971
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
Upvotes: 2
Views: 7301
Reputation: 76957
You need str.count
In [5285]: df.words.str.count("the answer").sum()
Out[5285]: 3
In [5286]: df.words.str.count("the answer")
Out[5286]:
0 1
1 2
2 0
3 0
Name: words, dtype: int64
Upvotes: 8