mask and then fillna

Solutions on MaxInterview for mask and then fillna by the best coders in the world

showing results for - "mask and then fillna"
Gannon
12 Nov 2016
1a b c 
21 1 5.0
31 1 None
41 1 4.0
51 2 1.0
61 2 1.0
71 2 4.0 
82 1 3.0
92 1 2.0
102 1 None
112 2 3.0
122 2 4.0
13
14mask = (df['a']==1) & (df['b']==1)
15mean = df.loc[mask, 'c'].mean()
16df.loc[mask, 'c'] = df.loc[mask, 'c'].fillna(mean)
17
18df['c'] = df['c'].mask(mask, df['c'].fillna(mean))
19#similar
20#df['c'] = np.where(mask, df['c'].fillna(mean), df['c'])
21
22print (df)
23    a  b    c
240   1  1  5.0
251   1  1  4.5
262   1  1  4.0
273   1  2  1.0
284   1  2  1.0
295   1  2  4.0
306   2  1  3.0
317   2  1  2.0
328   2  1  NaN
339   2  2  3.0
3410  2  2  4.0
queries leading to this page
mask and then fillnamask and then fillna