I have a pandas DataFrame and the requirement is to fill the NaN value in a row with by dividing sum of before missing value and after missing value by 3 and adding the result to the before missing value (This fills first Row Nan value). Second NaN is to be filled with sum of before missing value and after missing value by 3 (same as previous) but to add the result to the first filled NaN Value.
df = pd.DataFrame({'val': [1,np.nan,np.nan, 4, 5, np.nan, np.nan, 11, 1,2,6, np.nan, np.nan, 15]})
and the expected result is:
df = pd.DataFrame({'val': [1,2,3, 4, 5, 7, 9, 11, 1,2,6, 9, 12, 15]})
CodePudding user response:
Your logic is actually what interpolate is doing by default (method='linear'):
df.interpolate()
Output:
val
0 1.0
1 2.0
2 3.0
3 4.0
4 5.0
5 7.0
6 9.0
7 11.0
8 1.0
9 2.0
10 6.0
11 9.0
12 12.0
13 15.0
