Is there a way to calculate between two value of NaN value?

Question:

i’m having a trouble to code into finding in between values in pandas dataframe.
the dataframe:

value
30
NaN
NaN
25
NaN
20
NaN
NaN
NaN
NaN
15

the formula is like this:

value before nan - ((value before nan - value after nan)/div by no. of nan in between the values)

example of expected value should be like this:

30 - (30-25)/2 = 27.5
27.5 - (27.5-25)/1 = 25

so the expected dataframe will look like this:

value expected value
30 30
NaN 27.5
NaN 25
25 25
NaN 20
20 20
NaN 18.75
NaN 17.5
NaN 16.25
NaN 15
15 15
Asked By: Nazmi Husaini

||

Answers:

IIUC, you can generalize your formula into two parts:

  1. Any nan right before a non-nan is just same as that number
  • {value-before-nan} – ({value-before-nan} – {value-after-nan})/1 = {value-after-nan}
  1. Rest of nan are linear interpolation.

So you can use bfill with interpolate:

df.bfill(limit=1).interpolate()

Output:

    value
0   30.00
1   27.50
2   25.00
3   25.00
4   20.00
5   20.00
6   18.75
7   17.50
8   16.25
9   15.00
10  15.00
Answered By: Chris
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.