title: How to calculate vwap (volume weighted average price) using groupby and apply?
body: <p>I have read multiple post similar to my question, but I still can't figure it out. I have a pandas df that looks like the following (for multiple days): </p>
  
  <pre><code>Out[1]: 
                       price  quantity
  time                                
  2016-06-08 09:00:22  32.30    1960.0
  2016-06-08 09:00:22  32.30     142.0
  2016-06-08 09:00:22  32.30    3857.0
  2016-06-08 09:00:22  32.30    1000.0
  2016-06-08 09:00:22  32.35     991.0
  2016-06-08 09:00:22  32.30     447.0
  ...
  </code></pre>
  
  <p>To calculate the vwap I could do:</p>
  
  <pre><code>df['vwap'] = (np.cumsum(df.quantity * df.price) / np.cumsum(df.quantity))
  </code></pre>
  
  <p>However, I would like to start over every day (groupby), but I can't figure out how to make it work with a (lambda?) function.</p>
  
  <pre><code>df['vwap_day'] = df.groupby(df.index.date)['vwap'].apply(lambda ...
  </code></pre>
  
  <p>Speed is of essence. Would appreciate any help:) </p>