I have a pandas dataframe df as below:
A B
0 70.0 20
1 NaN 20
2 28.0 100
3 75.0 120
4 56.0 30
5 84.0 90
6 NaN 100
7 19.0 10
8 93.0 80
9 94.0 70
10 72.0 20
I am trying to change the values of A as an average value based on B’s A. For instance, for B = 20, I would like all A values to be an average of 70 and 72 ignoring NaN. What is the best way possible please? I am thinking along the groupby lines as in…
df['AA']=df.groupby('B')['A'].transform(lambda s: s=s.mean())
That did not help.