I have a data frame with a column named SAM with following data
SAM
3
5
9
Nan
Nan
24
40
Nan
57
Now I want to Insert 12
,
You can try this vectorized approach:
nul = df['SAM'].isnull()
nul.groupby((nul.diff() == 1).cumsum()).cumsum()*3 + df['SAM'].ffill()
#0 3.0
#1 5.0
#2 9.0
#3 12.0
#4 15.0
#5 24.0
#6 40.0
#7 43.0
#8 57.0
#Name: SAM, dtype: float64
SAM
column to the result.