2

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

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

WOW, so quick :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.