I have a xls file that I imported as a pandas dataframe. It has NaN values; how do I set-up a function that replaces NaN with the interpolation between the adjacent values? I can't use pd.DataFrame.interpolate or any existing interpolation function since I'm supposed to make my own function.
Here's what I have but I think this is very wrong. Sorry, still very new to Python :(
import pandas as pd
file = pd.read_excel("xls file")
def interpolate(x):
for i in range(len(x)):
if x.iloc[i, -1].isnull():
x.iloc[i,-1] = (((x.iloc[i-1, -1]) + (x.iloc[i+1, -1]))/2)
else:
x.iloc[i,-1] = x.iloc[i, -1]
interpolate(file)
So for example the dataframe would originally look like this:
0 1.04
1 0.99
2 NaN
3 1.05
4 1.05
I want it to return:
0 1.04
1 0.99
2 1.02
3 1.05
4 1.05
For this, assume that there are no consecutive NaN entries