title: pandas complex groupby, count and apply a cap
body: <p>Sample dataframe</p>
  
  <pre><code>&gt; 0   location_day       payType    Name   ratePay      elapsedSeconds   
  &gt; 1   2019-12-10           PRE       Amy    12.25             199 
  &gt; 2   2019-12-12           PRE       Amy    12.25               7 
  &gt; 3   2019-12-17           PRE       Amy    12.25              68 
  &gt; 4   2019-12-17            RP       Amy     8.75              62
  </code></pre>
  
  <p>For each day, sum elapsedSeconds and calculate new column with total toPay (elapsedSeconds * ratePay) but apply a "cap" elapsedSeconds of 120. For any single day that only has 1 payType, apply cap so that only 120 is used to calculate "toPay" col. </p>
  
  <p>But...</p>
  
  <p>Also, groupby payType so that if there are 2 unique "payTypes" on a single day, sum the elapsedSeconds to determine if it's over the cap (120) and if so, subtract the elapsedSeconds from the last payType to make the sum equal to 120.</p>
  
  <p>So I desire this output:</p>
  
  <pre><code>&gt; 0   location_day       payType    Name   ratePay      elapsedSeconds  
  &gt; 1   2019-12-10           PRE       Amy    12.25             120    
  &gt; 2   2019-12-12           PRE       Amy    12.25               7  
  &gt; 3   2019-12-17           PRE       Amy    12.25              68  
  &gt; 4   2019-12-17            RP       Amy     8.75              52
  </code></pre>
  
  <p>I'm not quite sure how to approach this one and really only have performed some very basic grouping and testing of calculating new columns with conditional statements such as</p>
  
  <pre><code>finDfcalc1 = finDf.sort('location_day').groupby(flds)['elapsedSeconds'].sum().reset_index()
  finDfcalc1.loc[finDfcalc1['elapsedSeconds'] &lt; 120, 'elapsedSecondsOverage'] = finDfcalc1['elapsedSeconds'] * 1
  finDfcalc1.loc[finDfcalc1['elapsedSeconds'] &gt; 120, 'elapsedSecondsOverage'] = finDfcalc1['elapsedSeconds'] - 120
  finDfcalc1['toPay'] = finDfcalc1['ratePay'] * finDfcalc1['elapsedSecondsOverage']
  </code></pre>
  
  <p>None of this has to be a one-liner and would be perfectly happy just working out the logic. All suggestions and ideas are greatly appreciated.</p>