title: Pandas cumulative conditional sum by dates
body: <p>Imagine a pandas DataFrame like this</p>
  
  <pre><code>      date  id  initial_value  part_value
  2016-01-21   1            100          10
  2016-05-18   1            100          20
  2016-03-15   2            150          75
  2016-07-28   2            150          50
  2016-08-30   2            150          25
  2015-07-21   3             75          75
  </code></pre>
  
  <p>Generated with following</p>
  
  <pre><code>df = pd.DataFrame({
      'id': (1, 1, 2, 2, 2, 3),
      'date': tuple(pd.to_datetime(date) for date in
                    ('2016-01-21', '2016-05-18', '2016-03-15', '2016-07-28', '2016-08-30', '2015-07-21')),
      'initial_value': (100, 100, 150, 150, 150, 75),
      'part_value': (10, 20, 75, 50, 25, 75)}).sort_values(['id', 'date'])
  </code></pre>
  
  <p>I wish to add a column with the remaining value defined by the <code>initial_value</code> minus the cumulative sum of <code>part_value</code> conditioned on id and dates before. Hence I wish my goal is</p>
  
  <pre><code>      date  id  initial_value  part_value  goal
  2016-01-21   1            100          10   100
  2016-05-18   1            100          20    90
  2016-03-15   2            150          75   150
  2016-07-28   2            150          50    75
  2016-08-30   2            150          25    25
  2015-07-21   3             75          75    75
  </code></pre>
  
  <p>I'm thinking that a solution can be made by combining the solution from <a href="https://stackoverflow.com/questions/40159490/pandas-cumulative-sum-of-one-column-based-on-value-of-another">here</a> and <a href="https://stackoverflow.com/questions/37601017/pandas-conditional-cumulative-sum">here</a>, but I can't exactly figure it out.</p>