Search code examples
pythonpandascountpython-applymap

Count and map number of appearances of strings


I am mapping specific keywords with text data using applymap in Python. Let's say I want to check how often the keyword "hello" matches with the text data over all rows. Applymap gives me the desired matrix outcome, however only a "True" or "False" instead of the number of appearances.

I tried to connect count() with my applymap function, but I could not make it work.

The minimal working example is as follows:

import pandas as pd
import numpy as np

df = pd.DataFrame({'text': ['hello hello', 'yes no hello', 'good morning']})
keys = ['hello']
keyword = pd.DataFrame({0:keys})

res = []
for a in df['text']:
    res.append(keyword.applymap(lambda x: x in a))

map = pd.concat(res, axis=1).T
map.index = np.arange(len(map))

#Output
map
       0
0   True
1   True
2  False

#Desired Output with 'hello' appearing twice in the first row, once in the second and zero in the third of df.
   0
0  2
1  1
2  0

I am looking for a way to keep my applymap function to obtain the matrix form, but replace the True (1) and False (0) with the number of appearances, such as the desired output shows above.


Solution

  • Instead of testing for an item in the list:

    res.append(keyword.applymap(lambda x: x in a)) # x == a

    You should use:

    res.append(keyword.applymap(lambda x: str.count(a, x))) # counting occurrence of "a"