Lets say I have a pd.DataFrame
:
df = pd.DataFrame(zip(['A','A','B','B'],[1, 2, 3, 4]), columns=['Group','Value'])
Group Value
0 A 1
1 A 2
2 B 3
3 B 4
and I would like to calculate the average value for each group and replace all values of the group by the average. How do I do that in pandas?
The result will be like :
Group Value
0 A 1.5
1 A 1.5
2 B 3.5
3 B 3.5