Pandas comparison cannot come out right result

Viewed 30

I have a question with the following simple pandas comparison:

list = [1,1,0,1,-1,0,1,0,-1]
df = pd.DataFrame(list, columns=['x'])

df['y'] = np.where(((df['x'] + df['y'].shift(1))  <= 0), df['y'].shift(1), df['x'] + df['y'].shift(1) )

Everything is fine, except x = 0,

df['x'][2] = 0.0
df['y'][1] = 1.0
df['x'][2]+df['y'][1] should be 1.0, but the result as follows is 0.0


`x` `y` 
`1.0`   `NaN`
`1.0`   `1.0`
`0.0`   `0.0`
`1.0`   `1.0`
`-1.0`  `0.0`   
`0.0`   `0.0`
`1.0`   `1.0`
`1.0`   `1.0`
`-1.0`  `0.0`   

I expect the result is

y NaN 1.0 1.0 1.0 0,0 0,0 1.0 1.0 0.0

Any idea?

0 Answers
Related