title: Apply function to two columns Pandas
body: <p>I'm trying to apply a function that calculates the R2 score to two columns of a pandas data frame. It works if first I group by some other column but when applied to the whole dataframe it fails.</p>
  
  <p>This is the code that works:</p>
  
  <pre><code>from sklearn.metrics import r2_score
  df = pd.DataFrame({'cat':['A','A','B','B'], 'num1':[.1,.2,.3,.4], 
  'num2': [.1,.2,.3,.4]})
  
  df.groupby('cat').apply(lambda x: r2_score(x['num1'],x['num2']))
  </code></pre>
  
  <p>With this output:</p>
  
  <pre><code>cat
  A    1.0
  B    1.0
  dtype: float64
  </code></pre>
  
  <p>But this doesn't work: </p>
  
  <pre><code>df.apply(lambda x: r2_score(x['num1'],x['num2']))
  </code></pre>
  
  <p>With a key error:
      KeyError: ('num1', 'occurred at index cat')</p>
  
  <p>Could anyone explain why this is the case clarifying the inner workings of the apply function and provide a solution for the latter apply?</p>