I have a df as below:

I would like to group by id and flag and create a new column in the df which is the result of: [sum(value1)/sum(value2)] * 12. Therefore I will need the result to be:

I have created a function:

`def calculation (value1, value2): result = (value1/value2) * 12 return(result)`

Could you advise which is the best way to apply this function along with the grouping, in order to get the desired output? Many thanks

## Answer

The following code should work.

import pandas as pd df = pd.DataFrame({"id" : [1,1,2,2],"flag":["A","B","B","A"],"value1":[520,200,400,410],"value2":[12,5,11,2]}) def calculation(value1, value2): result = (value1/value2) * 12 return(result) df.groupby(['id','flag']).apply(lambda x: calculation(x['value1'],x['value2'])).astype(int)

You just have to use the following for groupby and apply.

df.groupby(['id','flag']).apply(lambda x: calculation(x['value1'],x['value2'])).astype(int)