Suppose I have a DataFrame with some NaN -
import pandas as pd l = [{'C1':-6,'C3':2}, {'C2':-6,'C3':3}, {'C1':-6.3,'C2':8,'C3':9}, {'C2':-7}] df1 = pd.DataFrame(l, index=['R1','R2','R3','R4']) print(df1) C1 C2 C3 R1 -6.0 NaN 2.0 R2 NaN -6.0 3.0 R3 -6.3 8.0 9.0 R4 NaN -7.0 NaN Problem - If there is any NaN value in any row cell then it has to be replaced by the aggregate of non-null values from the same row. For instance, in first row, the value of (R1,C2) should be = (-6+2)/2 = -2
Expected output -
C1 C2 C3 R1 -6.0 -4.0 2.0 R2 -1.5 -6.0 3.0 R3 -6.3 8.0 9.0 R4 -7.0 -7.0 -7.0