I am new to Python and fixated on how to conditionally shift values. I have successfully used the shift function when I just need to create a new column. However, this does not work with the function.
Original df:
BEGIN SPEED SPEED_END
322 28 0
341 0 23
496 5 1
500 0 0
775 0 0
979 0 0
1015 0 0
1022 0 14
1050 11 6
I want the value to BEGINbe changed to the previous value of the record BEGINand the value SPEEDthat should be changed to the previous value of the record SPEEDin the records, where the SPEED=0previous one is SPEED_END=0,
So, the above table should be:
BEGIN SPEED SPEED_END
322 28 0
322 28 23
496 5 1
500 0 0
500 0 0
500 0 0
500 0 0
500 0 14
1050 11 6
I have tried many different things. I have currently tried:
def cont(row,param):
if row['SPEED'] == 0 and row['SPEED_END'].shift(1) == 0:
val = row[param].shift(1)
else:
val = row[param]
return val
df['BEGIN'] = df.apply(cont, param='BEGIN', axis=1)
But this gives me an error:
AttributeError: (the 'float' object does not have the 'shift' attribute, u'occurred at index 0 ')
Any suggestions are welcome!