title: Pandas: Add amount in Dataframe based on date ranges and IDs
body: <p>I asked a question a while back (<a href="https://stackoverflow.com/questions/41314324/sum-large-pandas-dataframe-based-on-smaller-date-ranges">here</a>) about how to find the sum of a column in a dataframe by comparing dates in another dataframe.  However, I now want to do something similar but include an ID that may have an exact matching date with another ID.  </p>
  
  <p>Here is my code:</p>
  
  <pre><code>df_a = pd.DataFrame({
          'end':pd.to_datetime(['1/15/2016','1/15/2016','3/15/2016','5/15/2016','5/15/2016','7/15/2016']),
          'ID':[1,2,1,2,1,1]
      })
  
  df_a['start'] = df_a.groupby('ID')['end'].shift().fillna(0)
  df_a = df_a[['start','end','ID']]
  
  df_b = pd.DataFrame({
          'date':pd.to_datetime(['1/1/2016','1/1/2016','2/1/2016','2/1/2016','3/1/2016','3/1/2016','6/1/2016','6/1/2016','7/1/2016','7/1/2016','8/1/2016']),
          'ID':       [1, 2, 1,  2,  1,   2,   2,    1,    1,     2,     2],
          'amount':   [1, 2, 10, 20, 100, 200, 2000, 1000, 10000, 20000, 200000]
      })
  </code></pre>
  
  <p>And my desired output:</p>
  
  <pre><code>         start        end   ID   amount
  0   1970-01-01 2016-01-15    1        1
  1   1970-01-01 2016-01-15    2        2
  2   2016-01-15 2016-03-15    1      110
  3   2016-01-15 2016-05-15    2      220
  4   2016-03-15 2016-05-15    1        0
  5   2016-05-15 2016-07-15    1    11000
  </code></pre>
  
  <p>I've tried <code>merge()</code>, <code>merge_asof()</code>, <code>combine_first()</code>, <code>groupby()</code> and gotten close, but not all the way.</p>
  
  <p>Here is a non-pandas version that works, but for a large data set, I imagine this would be quite slow:</p>
  
  <pre><code>amount = []
  for s, e, i in zip(df_a['start'], df_a['end'], df_a['ID']):
      amount.append(df_b['amount'][(s &lt; df_b['date']) &amp; (df_b['date'] &lt;= e) &amp; (df_b['ID'] == i)].sum())
  
  df_a['amount'] = pd.Series(amount)
  </code></pre>
  
  <p>Would love some help, thanks in advance.</p>