1df['three'] = df.groupby(['one','two'], sort=False)['three']
2 .apply(lambda x: x.ffill().bfill())
3print (df)
4 one two three
50 1 1 10.0
61 1 1 10.0
72 1 1 10.0
83 1 2 20.0
94 1 2 20.0
105 1 2 20.0
116 1 3 NaN
127 1 3 NaN
1print (df)
2 one two three
30 1 1 10.0
41 1 1 40.0
52 1 1 NaN
63 1 2 NaN
74 1 2 20.0
85 1 2 NaN
96 1 3 NaN
107 1 3 NaN
11
12df['three'] = df.groupby(['one','two'], sort=False)['three']
13 .apply(lambda x: x.fillna(x.mean()))
14print (df)
15 one two three
160 1 1 10.0
171 1 1 40.0
182 1 1 25.0
193 1 2 20.0
204 1 2 20.0
215 1 2 20.0
226 1 3 NaN
237 1 3 NaN