Search code examples
pythonpandasvectormultiplicationpandas-groupby

Pandas groupby: get product value for each individual group


This might sound a little stupid so apologies in advance. I have dataframe such that:

            value       date
0           1.0         2010-01-31
1           0.3         2010-02-28
2           1.6         2011-03-31
3           2.5         2011-04-30
4          -1.0         2012-05-31
5          -0.3         2012-06-30
6           1.6         2013-07-31

I want to group it by year and the apply the follwoing formula to each group [[(1 + v1) * (1 + v2) * (1 + v3) *....(1 +vn)] - 1] * 100 So the operation is to add 1 to each individual element within a group, then take its product, then subtract 1 from the resulting value and then multiply that by 100

But when i do this

df.groupby(a.date.dt.year).apply(lambda x: (1+x['value'])).prod()

I get a single value ( I think it gets the product of everything. How do I get the product for each group (a single value for each year)


Solution

  • You can do it outside the groupby apply function , with assign

    df.assign(val=df.value+1).groupby(df.date.dt.year).val.prod()-1
    Out[800]: 
    date
    2010    1.6
    2011    8.1
    2012   -1.0
    2013    1.6
    Name: val, dtype: float64
    

    Two benefits:

    1, More easy to understand

    2, More efficient