groupby weighted average and sum in pandas dataframe

后端 未结 4 2027
暖寄归人
暖寄归人 2020-11-27 12:26

I have a dataframe ,

    Out[78]: 
   contract month year  buys  adjusted_lots    price
0         W     Z    5  Sell             -5   554.85
1         C              


        
4条回答
  •  无人及你
    2020-11-27 12:57

    Wouldn't it be a lot more simpler to do this.

    1. Multiply (adjusted_lots * price_weighted_mean) into a new column "X"
    2. Use groupby().sum() for columns "X" and "adjusted_lots" to get grouped df df_grouped
    3. Compute weighted average on the df_grouped as df_grouped['X']/df_grouped['adjusted_lots']

提交回复
热议问题