0

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.

XsLiar
  • 35
  • 7
  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Apr 09 '19 at 08:52
  • [Please don't post images of code/data (or links to them)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) – jezrael Apr 09 '19 at 09:03
  • For counting certain elements you could use the histogram-procedure. See: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.hist.html – pyano Apr 09 '19 at 09:45
  • You might want to take in consideration that `map` is a [build-in name](https://docs.python.org/3/library/functions.html) for python. In your script you basically overwrite its build-in function. Preferably you use ***result*** instead. – ZF007 Apr 09 '19 at 10:36

1 Answers1

2

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"

ZF007
  • 3,708
  • 8
  • 29
  • 48