title: Why do I need lambda to apply functions to a Pandas Dataframe?
body: <p>I have a Pandas data frame and am attempting to pass a function over the entries in one column using the apply() function.</p>
  
  <p>My function is of the form:</p>
  
  <pre><code>def foo(Y):
      #accepts a pandas data frame
      #carries out some search on the text in each row of the dataframe
      #groups successful searches
      #return a new column as a pandas series
  </code></pre>
  
  <p>My dataframe is of the form:</p>
  
  <pre><code>    Info    WN    RN
  0    XX    YY    ZZ    
  1    AA    BB    CC
  2    JJ    KK    LL
  </code></pre>
  
  <p>I attempt to execute:</p>
  
  <pre><code>df['SR'] = (df['Info'].apply(foo(x)))
  </code></pre>
  
  <p>My error is as follows:</p>
  
  <pre><code>File "&lt;ipython-input-11-ae54015436d8&gt;", line 1, in &lt;module&gt;
  df['SR'] = (df['Info'].apply(foo(x))
  NameError: name 'x' is not defined
  </code></pre>
  
  <p>But if I use:</p>
  
  <pre><code>df['SR'] = (df['Info'].apply(lambda x:foo(x)))
  </code></pre>
  
  <p>It works fine.</p>
  
  <p>I understand how Lambda works (at least I thought I did). I don't understand why I need it.</p>
  
  <p>Why do I need lambda to successfully pass the function over the data frame? Shouldn't the apply() function do that by definition? </p>
  
  <p>Or is it that I am effectively doing it the other way around i.e. passing my data frame into the function, and returning some output, rather than iteratively applying the function to the data frame (if that makes sense)?</p>
  
  <p>Can anyone offer any insight? </p>
  
  <p>My sincere thanks!</p>