Count appearances of a string throughout columns in pandas

冷暖自知 提交于 2019-12-12 05:57:27

问题


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:


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


来源:https://stackoverflow.com/questions/46893629/count-appearances-of-a-string-throughout-columns-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!