title: pandas: calculate percentiles within subgroups?
body: <p>I've got a Pandas dataframe that looks like this:</p>
  
  <pre><code> school_id  uni_id  points
   123        44      180
   123        45      160
   123        45      160
   123        48      110
   124        44      180
   124        45      160
   124        47      130
   123        48      120
  </code></pre>
  
  <p>Generated as follows, to help kind answerers:</p>
  
  <pre><code>df = pd.DataFrame({ 
      'school_id': [123, 123, 123, 123, 124, 124, 124, 124], 
      'school_id': [44, 45, 45, 48, 44, 45, 47, 48], 
      'points': [180, 160, 160, 110, 180, 160, 130, 120]
  })
  </code></pre>
  
  <p>I'd like to add a percentile column, which represents the percentile of the <code>points</code> value for each school. So this dataset would look like this:</p>
  
  <pre><code> school_id  uni_id  points  percentile
   123        44      180     100
   123        45      160     50
   123        45      160     50
   123        48      110     0
   124        44      180     100
   124        45      160     66
   124        47      130     33
   123        48      120     0
  </code></pre>
  
  <p>What's the best way to do this? I assume that I need to group by <code>school_id</code>, then do <code>df.quantile()</code> within each subgroup somehow, then ungroup?</p>
  
  <p><strong>UPDATE</strong>: Maybe I need to start with something like this... <code>df.groupby('school_id')['points'].rank(ascending=False)</code> and then divide the rank by the length of each group to normalise it between 0 and 100?</p>