title: Create a matrix of words occurring in a Pandas data frame with text strings
body: <p>I have a Pandas data frame with a column of text data. I want to compare each row of this text data with a list of words that I'm interested in. The comparison should result in a matrix that shows the occurrence of the word (0 or 1) in the text of that row of data.</p>
  
  <p>Input data frame:</p>
  
  <pre><code>text
  That bear talks
  The stone rocks
  Tea is boiling
  The bear drinks tea
  </code></pre>
  
  <p>Input list of words:</p>
  
  <pre><code>[bear, talks, tea]
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>text                 bear  talks  tea
  That bear talks      1     1      0
  The stone rocks      0     0      0
  Tea is boiling       0     0      1
  The bear drinks tea  1     0      1
  </code></pre>
  
  <p>I found some information on sklearn.feature_extraction.text.HashingVectorizer but from what I understand it just takes the whole data frame and breaks it down in component words and counts those. What I want to do is do it on a very limited list.</p>
  
  <p>With sklearn I did the following:</p>
  
  <pre><code>from sklearn.feature_extraction.text import HashingVectorizer
  
  countvec = HashingVectorizer()
  
  countvec.fit_transform(resultNLdf2.text)
  </code></pre>
  
  <p>But that gives me the following:</p>
  
  <pre><code>&lt;73319x1048576 sparse matrix of type '&lt;class 'numpy.float64'&gt;'
      with 1105683 stored elements in Compressed Sparse Row format&gt;
  </code></pre>
  
  <p>Which seems a bit big to work with unless I could select on the words I want from this sparse matrix but I don't know how to work with it.</p>
  
  <p>I'm sorry if I used the wrong words to explain this problem, not sure if you would call this a matrix for example.</p>
  
  <p><strong>edit</strong></p>
  
  <p>The true data i'm working on is rather large, 1264555 rows with strings of tweets. At least i've learned not to over simplify a problem :-p. This makes some of the given solutions (Thanks for trying to help!!) not work because of memory issues or just being extremely slow. This was also a reason I was looking at sklearn.</p>
  
  <p>with:</p>
  
  <pre><code>from sklearn.feature_extraction.text import CountVectorizer
  
  words = ['bear', 'talks', 'tea']
  
  countvec = CountVectorizer(vocabulary=words)
  
  countvec.fit_transform(resultNLdf2.text)
  </code></pre>
  
  <p>you can actually limit the words you want to look at by giving a simple list. But this leaves me with the problem that it is in a format I'm not sure what to do with as described above.</p>