say my column is something like this:
JavaScript
x
18
18
1
trade_signal
2
buy
3
buy
4
buy
5
buy
6
sell
7
sell
8
sell
9
sell
10
buy
11
buy
12
buy
13
sell
14
sell
15
buy
16
sell
17
buy
18
I would like to drop the duplicate elements in the column and replace them with NAN or 0 so it would end up with something like:
JavaScript
1
18
18
1
trade_signal
2
buy
3
nan
4
nan
5
nan
6
sell
7
nan
8
nan
9
nan
10
buy
11
nan
12
nan
13
sell
14
nan
15
buy
16
sell
17
buy
18
I am completely unsure of the logic I can use to do this, I think I would forward fill up until the next change in signal with NAN values somehow?
Advertisement
Answer
Try mask with shift:
JavaScript
1
4
1
df['trade_signal'] = df['trade_signal'].mask(df['trade_signal'].eq(
2
df['trade_signal'].shift())
3
)
4
JavaScript
1
18
18
1
trade_signal
2
0 buy
3
1 NaN
4
2 NaN
5
3 NaN
6
4 sell
7
5 NaN
8
6 NaN
9
7 NaN
10
8 buy
11
9 NaN
12
10 NaN
13
11 sell
14
12 NaN
15
13 buy
16
14 sell
17
15 buy
18