A1 A2 A3 B1 B2 B3
aa 1.0 4.0 9 NaN NaN 2.0
bb NaN NaN 9 9.0 2.0 2.0
cc 3.0 2.0 7 1.0 3.0 2.0
dd 4.0 5.0 7 NaN 4.0 5.0
ee 5.0 NaN 1 5.0 1.0 5.0
ff 3.0 5.0 2 5.0 2.0 NaN
I need to fill the missing values (Na) based on the following parameters:
if the row(aa, bb, cc, dd, ff) has a missing value for either of columns(A1, A2, A3 or B1, B2, B3), then fill the missing value with the mean of the values of the other two rows of either (A1-3 or B1-3) example: (dd:B1) should be (5+4)/2 = 4.5
if the rows have two missing values belong to either (A1-A3 or B1-B3) then fill it with the available value in the third column of the category(A or B) example: (bb:A1 and bb: A2) == 9 as (bb: A3 is 9)
Expected output:
A1 A2 A3 B1 B2 B3
aa 1.0 4.0 9.0 2.0 2.0 2.0
bb 9.0 9.0 9.0 9.0 2.0 2.0
cc 3.0 2.0 7.0 1.0 3.0 2.0
dd 4.0 5.0 7.0 4.5 4.0 5.0
ee 5.0 3.0 1.0 5.0 1.0 5.0
ff 3.0 5.0 2.0 5.0 2.0 3.5