How to calculate vwap (volume weighted average price) using groupby and apply?

后端 未结 1 2012
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 15:43

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):

Ou         


        
相关标签:
1条回答
  • 2020-12-30 16:22

    Option 0
    plain vanilla approach

    def vwap(df):
        q = df.quantity.values
        p = df.price.values
        return df.assign(vwap=(p * q).cumsum() / q.cumsum())
    
    df = df.groupby(df.index.date, group_keys=False).apply(vwap)
    df
    
                         price  quantity       vwap
    time                                           
    2016-06-08 09:00:22  32.30    1960.0  32.300000
    2016-06-08 09:00:22  32.30     142.0  32.300000
    2016-06-08 09:00:22  32.30    3857.0  32.300000
    2016-06-08 09:00:22  32.30    1000.0  32.300000
    2016-06-08 09:00:22  32.35     991.0  32.306233
    2016-06-08 09:00:22  32.30     447.0  32.305901
    

    Option 1
    Throwing in a little eval

    df = df.assign(
        vwap=df.eval(
            'wgtd = price * quantity', inplace=False
        ).groupby(df.index.date).cumsum().eval('wgtd / quantity')
    )
    df
    
                         price  quantity       vwap
    time                                           
    2016-06-08 09:00:22  32.30    1960.0  32.300000
    2016-06-08 09:00:22  32.30     142.0  32.300000
    2016-06-08 09:00:22  32.30    3857.0  32.300000
    2016-06-08 09:00:22  32.30    1000.0  32.300000
    2016-06-08 09:00:22  32.35     991.0  32.306233
    2016-06-08 09:00:22  32.30     447.0  32.305901
    
    0 讨论(0)
提交回复
热议问题