简体   繁体   中英

fillna by selected rows in pandas DataFrame

I have next pandas DataFrame:

a b c 
1 1 5.0
1 1 None
1 1 4.0
1 2 1.0
1 2 1.0
1 2 4.0 
2 1 3.0
2 1 2.0
2 1 None
2 2 3.0
2 2 4.0

I want to replace None , but not by the column mean. I want to select all rows, where the values in a and b are similar and if c has a None -values in selected rows, replace them only with the c -mean of selected rows. Something like (this code doesn't work):

df[df[('a'==1) & ('b'==1)]]['c'].fillna(df[df[('a'==1) & ('b'==1)]]['c'].mean())

which should get me the output:

a b c 
1 1 5.0
1 1 4.5
1 1 4.0
1 2 1.0
1 2 1.0
1 2 4.0 
2 1 3.0
2 1 2.0
2 1 None
2 2 3.0
2 2 4.0

You need filter values of c by conditions and assign back column c :

mask = (df['a']==1) & (df['b']==1)
mean = df.loc[mask, 'c'].mean()
df.loc[mask, 'c'] = df.loc[mask, 'c'].fillna(mean)

Or use mask for replace by conditions:

df['c'] = df['c'].mask(mask, df['c'].fillna(mean))
#similar
#df['c'] = np.where(mask, df['c'].fillna(mean), df['c'])

print (df)
    a  b    c
0   1  1  5.0
1   1  1  4.5
2   1  1  4.0
3   1  2  1.0
4   1  2  1.0
5   1  2  4.0
6   2  1  3.0
7   2  1  2.0
8   2  1  NaN
9   2  2  3.0
10  2  2  4.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM