I have a signal column and I want to create a NewSignal column that only takes the value of Signal when Allowed column is 1, otherwise it should keep its own previous value:
df:
Signal Allowed NewSignal
0 -1 0 0
1 -1 0 0
2 -1 0 0
3 -1 1 -1
4 -1 1 -1
5 -1 1 -1
6 -1 1 -1
7 -1 0 -1
8 -1 0 -1
9 1 0 -1
10 1 1 1
11 1 1 1
12 1 1 1
When I try to create NewSignal like this:
df['NewSignal'] = 0
df['NewSignal'] = np.where(df['Allowed'] == 1,df['Signal'],df['NewSignal'].shift(1))
NewSignal is NOT correct on row 7:
Signal Allowed NewSignal
0 -1 0 NaN
1 -1 0 0.0
2 -1 0 0.0
3 -1 1 -1.0
4 -1 1 -1.0
5 -1 1 -1.0
6 -1 1 -1.0
7 -1 0 0.0
8 -1 0 0.0
9 1 0 0.0
10 1 1 1.0
11 1 1 1.0
12 1 1 1.0