1

say my column is something like this:

trade_signal
buy
buy
buy
buy
sell
sell
sell
sell
buy
buy
buy
sell
sell
buy
sell
buy

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:

trade_signal
buy
nan
nan
nan
sell
nan
nan
nan
buy
nan
nan
sell
nan
buy
sell
buy

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?

1 Answer 1

2

Try mask with shift:

df['trade_signal'] = df['trade_signal'].mask(df['trade_signal'].eq(
                                            df['trade_signal'].shift())
                                             )

  trade_signal
0           buy
1           NaN
2           NaN
3           NaN
4          sell
5           NaN
6           NaN
7           NaN
8           buy
9           NaN
10          NaN
11         sell
12          NaN
13          buy
14         sell
15          buy
Sign up to request clarification or add additional context in comments.

5 Comments

thank you this is great, is it possible to fill nans up until the next buy/sell signal? so if it went buy,nan,nan,buy,nan,sell it would end up like buy,nan,nan,nan,nan,sell
@Jason_Leto can you give me an example series dict ? (also try the code once since I think this will handle that)
The code doesnt do that, for example df['Trade_Signal'] = pd.Series(np.array(['buy', 'NaN', 'Buy', 'Nan', 'Sell'])) i would like to be all nans between the first buy and last element(sell)
Sorry I used that as an example, changed it above. It gives me ['buy'',nan','buy',nan,'sell'] i would like ['buy'',nan''nan,nan,'sell']
@Jason_Leto just do df['trade_signal']=df['trade_signal'].ffill() before trying this code and let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.