title: Creating a calculated column, and appending it to dataframe
body: <p>My dataset (called 'train') is regarding mortgage applications. In order to improve my prediction of 'accepted' (0/1), I am trying to calculate 'lender acceptance ratio', and would like to append that new column to my dataset. </p>
  
  <p>The dataset is 500k rows, lender id varies from 0 to 6508, but not all of the id's (integers) in between actually exist. </p>
  
  <p>I have tried:</p>
  
  <p>Method 1:
  Creating a pivot table. This works: </p>
  
  <pre><code>train.pivot_table(index=['lender'], aggfunc='mean', values=['accepted'])
  </code></pre>
  
  <p>BUT: I can't seem to append the results of this pivot table to my dataset:</p>
  
  <ul>
  <li><p>Not directly, in one line of code. </p></li>
  <li><p>Not indirectly, by adding the results of this pivot table to an empty dataframe, and then merging with 'on=lender_id'.</p></li>
  </ul>
  
  <p>Method 2:
  Creating a new column with a groupby function. I used this: </p>
  
  <pre><code>train['lender_ar'] = train.groupby('lender').transform('mean')
  </code></pre>
  
  <p>, but am getting: </p>
  
  <pre><code>ValueError: Wrong number of items passed 21, placement implies 1
  </code></pre>
  
  <p>A bit of my dataset:</p>
  
  <pre><code>
  lender  accepted
  4536       1
  2458       0
  5710       1
  5888       1
  289        1
  
  </code></pre>
  
  <p>I hope someone can help! Thanks!</p>