Python shift column value with previous record value if record matches condition

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!

+4
2

mask ffill:

begin_cond = (df['SPEED'] == 0) & (df['SPEED_END'].shift(1) == 0)
df['BEGIN'] = df['BEGIN'].mask(begin_cond).ffill().astype(int)

, mask df['BEGIN'], begin_cond True NaN. ffill NaN df['BEGIN'].

:

   BEGIN  SPEED  SPEED_END
0    322     28          0
1    322      0         23
2    496      5          1
3    500      0          0
4    500      0          0
5    500      0          0
6    500      0          0
7    500      0         14
8   1050     11          6
+5

, .

df['begin_temp'] = df.begin.shift(1)
df['begin_shifted'] = df.ix[( df.SPEED== 0)  | (df.SPEED_END== 0), 'begin_temp']

df.ix[df.begin_shifted.isnull(),'begin_shifted'] = df.ix[df.begin_shifted.isnull(),'begin']
0

All Articles