I have a dataframe such as ::
Group COL1 COL2
G1 30 500
G1 21 500
G1 43 500
G2 89 677
G2 78 900
G3 32 322
G3 90 200
and I would like to add a new column called mean_group
where I calculate for each Group
, the sum of COL1
/unique value of COL2
.For instance, (30+21+43)/500 = 0.188
I should then get:
Group COL1 COL2 mean_group
G1 30 500 0.188
G1 21 500 0.188
G1 43 500 0.188
G2 89 677 0.2466765
G2 78 677 0.2466765
G3 32 322 0.09937888