title: python - pass dataframe column as argument in apply function
body: <p>I have the following dataframe:</p>
  
  <pre><code>In[1]: df = DataFrame({"A": ['I love cooking','I love rowing'], "B": [['cooking','rowing'],['cooking','rowing']]})
  </code></pre>
  
  <p>Thus the output that I get is:</p>
  
  <pre><code>In[2]: df
  Out[1]: 
              A                  B
  0  I love cooking  [cooking, rowing]
  1   I love rowing  [cooking, rowing]
  </code></pre>
  
  <p>I want to create a 'C' column where I count the number of occurrences of elements of 'B' in 'A'.</p>
  
  <p>The function I create is:</p>
  
  <pre><code>def count_keywords(x,y):
      a = 0
      for element in y:
          if element in x:
              a += 1
  return a
  </code></pre>
  
  <p>and then do:</p>
  
  <pre><code>df['A'].apply(count_keywords,args=(df['B'],))
  </code></pre>
  
  <p>In this case, I am passing the entire pandas dataseries as argument, so the element of the dataseries df['B'] is obviously a list, not a string (which in turn is the element of the list).</p>
  
  <p>So I get:</p>
  
  <pre><code>TypeError: 'in &lt;string&gt;' requires string as left operand, not list
  </code></pre>
  
  <p>However, if I adjust the function so that:</p>
  
  <pre><code>def count_keywords(x,y): 
      a = 0
      for element in y:
          for new_element in element:
              if new_element in x:
                  a += 1
      return a
  </code></pre>
  
  <p>and then do:</p>
  
  <pre><code>In[3]: df['A'].apply(count_keywords,args=(df['B'],))
  </code></pre>
  
  <p>the output is:</p>
  
  <pre><code>Out[2]: 
  0    2
  1    2
  </code></pre>
  
  <p>Because the function loops through every element in the pandas series and then through every element in the list.</p>
  
  <p>How can I get the function to just check, per dataframe row, the element of series df['B'] against the element in series df['A'], so the output is:? </p>
  
  <pre><code>Out[2]: 
  0    1
  1    1
  </code></pre>
  
  <p>Thanks a lot!</p>