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