Starting with a pandas data frame where the first column is made up of comment strings with the other columns being features of single words. For each row I would like to get a count of how many times each word appears in that row's comments cell. I have the list of words (feature columns) as a list called, "wordList" and am trying something like this but having trouble getting it working and the counts back into the data frame:
def word_count(comments):
for word in wordList:
return comment.count(word)
df.comments.apply(word_count)
What I have:
comments | hello | this | is | the | comments | blah |
--------------------------------------------------------------------------
this is the 1st | | | | | |
comments here | | | | | |
--------------------------------------------------------------------------
the 2nd comment | | | | | |
is is here this | | | | | |
What I want:
comments | hello | this | is | the | comments | blah |
--------------------------------------------------------------------------
this is the 1st | 0 | 1 | 2 | 1 | 1 | 0
comments is here| | | | | |
--------------------------------------------------------------------------
the 2nd comment | 0 | 1 | 2 | 2 | 0 | 0
is is here the | | | | | |