0

Is A.fillna(method='ffill') used to replace the succeeding value? How can we replace the succeeding value with minus -1 in each step?

curveball
  • 4,320
  • 15
  • 39
  • 49
Minou92
  • 137
  • 7
  • for example : in 20/03 / 2017 I have a value 120 .. and 20/ 04/2017 i have 100 .. so between 21/03/2017 and 19/04/2017,missinb value So I wont to fill in it with 120-1...120-N till 19/04/2017 – Minou92 Sep 28 '18 at 12:08
  • first A.fillna(method='ffill') then A.replace(np.nan, '-1) ? – Charles R Sep 28 '18 at 12:09
  • for more clarity and benefit to other users ,who can take away some learnings, can you add a sample df and sample output df please. :) – anky Sep 28 '18 at 12:17
  • for example : A.fillna(method='ffill') ,replace the forward value, for example : 20/03/2017==>120, I want to get in 21/03/2017 ==> 119.....till 19/04/2017, Then starting from 21/04/2017==> it will 100-1= 999 – Minou92 Sep 28 '18 at 12:18
  • Please edit your question to contain the relevant information. Other users shouldn't have to read through all the comments to understand what you are asking. – Roland Weber Sep 28 '18 at 13:11
  • @RolandWeber: for example : I have, df[0]= 420, NaN, 455; NaN,NaN, NaN, then using : df[0].isnull().astype(int)=` 0,1,0,1,1,1 `df[0].fillna(method='ffill') - df[0].isnull().astype(int)` df[0]=420,419,455,454;454,454 I am looking for to get 0,1,0,1,2,3, then in the ends : `df[0]= 420, 419, 455; 454,453, 452 ? – Minou92 Sep 28 '18 at 13:17

1 Answers1

0

Create the sample df:

z=pd.DataFrame(np.linspace(1,10,10),columns=['test'])
z.iloc[2]=np.nan
z.iloc[7:]=np.nan
print(z)
   test
0   1.0
1   2.0
2   NaN
3   4.0
4   5.0
5   6.0
6   7.0
7   NaN
8   NaN
9   NaN

Create a proxy column for recurring nans:

z['proxy'] = z.test.isnull().cumsum().diff().fillna(0)!=0
z['proxy'] = -1*(z.proxy.cumsum() -z.proxy.cumsum().where(~z.proxy).ffill().fillna(0))

Recalculate the z.test:

z['test']=z.proxy+z.test.ffill()

Resulting df:

test  proxy
0  1.0   -0.0
1   2.0   -0.0
2   1.0   -1.0
3   4.0   -0.0
4   5.0   -0.0
5   6.0   -0.0
6   7.0   -0.0
7   6.0   -1.0
8   5.0   -2.0
9   4.0   -3.0

Drop the proxy:

z=z.drop(columns=['proxy'])