groupby weighted average and sum in pandas dataframe

后端 未结 4 2026
暖寄归人
暖寄归人 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 13:05

    The solution that uses a dict of aggregation functions will be deprecated in a future version of pandas (version 0.22):

    FutureWarning: using a dict with renaming is deprecated and will be removed in a future 
    version return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)
    

    Use a groupby apply and return a Series to rename columns as discussed in: Rename result columns from Pandas aggregation ("FutureWarning: using a dict with renaming is deprecated")

    def my_agg(x):
        names = {'weighted_ave_price': (x['adjusted_lots'] * x['price']).sum()/x['adjusted_lots'].sum()}
        return pd.Series(names, index=['weighted_ave_price'])
    

    produces the same result:

    >df.groupby(["contract", "month", "year", "buys"]).apply(my_agg)
    
                              weighted_ave_price
    contract month year buys                    
    C        Z     5    Sell          424.828947
    CC       U     5    Buy          3328.000000
    SB       V     5    Buy            11.637500
    W        Z     5    Sell          554.850000
    

提交回复
热议问题