Replace pandas null value with ffill non-zero if subsequent value is nonzero

I need to replace the data line "0" in pandas with the previous lines without a non-null IF value and ONLY IF, the value in the line after "0" is not null.

those.

101
92
78
0
107
0
0

      

will become:

101
92
78
78
107
0
0

      

Any ideas how to do this would be much appreciated :-)

Thank!

+3


source to share


2 answers


using shift

you could do



In [608]: df.loc[(df.val == 0) & (df.val.shift(-1) != 0), 'val'] = df.val.shift(1)

In [609]: df
Out[609]:
     val
0  101.0
1   92.0
2   78.0
3   78.0
4  107.0
5    0.0
6    0.0

      

+4


source


This answer is similar to JohnGalt , but compares faster:



In [12]: np.where((df.Val.values==0)&(df.Val.shift(-1)!=0),df.Val.shift(),df.Val)
Out[31]: array([ 101.,   92.,   78.,   78.,  107.,    0.,    0.])

In [24]: %timeit np.where((df.Val.values==0)&(df.Val.shift(-1)!=0),df.Val.shift(),df.Val)
1000 loops, best of 3: 671 ยตs per loop

In [25]: %timeit df.loc[(df.Val == 0) & (df.Val.shift(-1) != 0), 'val'] = df.Val.shift(1)
100 loops, best of 3: 2.01 ms per loop

      

+1


source







All Articles