title: Pandas groupby a column and apply function to create a new column
body: <p>I am trying to set categories based on the grouping of members with the same admitting code and their mean length of stay value.</p>
  <p>By this I mean, I have the following data frame:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>MemberID</th>
  <th>AdmittingCode</th>
  <th>LOS</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>a</td>
  <td>5</td>
  </tr>
  <tr>
  <td>2</td>
  <td>a</td>
  <td>10</td>
  </tr>
  <tr>
  <td>1</td>
  <td>b</td>
  <td>2</td>
  </tr>
  <tr>
  <td>2</td>
  <td>b</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Now, in the above data frame I want to group based on admitting code and take the mean of LOS for that particular admitting code and if LOS is less than the mean it would be set as '0' category or else '1'.</p>
  <p>So, for the above case for admitting code 'a', we have LOS as 5 and 10. Here, the mean is 7.5 so the MemeberID of 1 with AdmittingCode as 'a' with LOS '5' would be set as category 0. Similarly with the logic the following data frame is acquired:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>MemberID</th>
  <th>AdmittingCode</th>
  <th>LOS</th>
  <th>LOSCategory</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>a</td>
  <td>5</td>
  <td>0</td>
  </tr>
  <tr>
  <td>2</td>
  <td>a</td>
  <td>10</td>
  <td>1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>b</td>
  <td>2</td>
  <td>1</td>
  </tr>
  <tr>
  <td>2</td>
  <td>b</td>
  <td>1</td>
  <td>0</td>
  </tr>
  </tbody>
  </table>
  </div>