1

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
ALollz
  • 57,915
  • 7
  • 66
  • 89
motam79
  • 3,542
  • 5
  • 34
  • 60

0 Answers0