'Fill in missing values in data table with linear interpolation? (python)
Say I have a data set like this:
DAY DATA
1 4
2 6
3 8
4 6
5 nan
6 nan
7 nan
8 2
9 4
10 nan
11 6
And I would like to fill in the nan values in the DATA column with the y = mx+b line connecting the two nearest non-nan values on either side. For instance, days 5, 6, and 7 would have the values 5, 4, and 3 respectively, and 10 would have the value 5. How could I do this with a for loop, for instance? Or any other way?
It seems pretty simple, here is what I have tried so far:
for i in range(len(DAY)):
if math.isnan(DATA[i]):
for j in range(i+1,len(DAY)):
if not math.isnan(DATA[j]):
print(DATA[i-1],DATA[j])
I am just trying to get it to print the two values on either side of the nan sequences (to start), but that does not work, instead it gives me the first of the nan sequence then every other number after the nan sequence until then next nans, etc...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|