pandas fill nan with mean of the groupby

Solutions on MaxInterview for pandas fill nan with mean of the groupby by the best coders in the world

showing results for - "pandas fill nan with mean of the groupby"
Leo
17 Sep 2016
1>>> df
2  name  value
30    A      1
41    A    NaN
52    B    NaN
63    B      2
74    B      3
85    B      1
96    C      3
107    C    NaN
118    C      3
12>>> df["value"] = df.groupby("name").transform(lambda x: x.fillna(x.mean()))
13>>> df
14  name  value
150    A      1
161    A      1
172    B      2
183    B      2
194    B      3
205    B      1
216    C      3
227    C      3
238    C      3