Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing the Duplicate occurrences from a Pandas series without removing entire rows

So I am working on testing financial performance of some stocks. I have a pandas column "signal" that by certain logic has "buy" or "sell" signals.

Now these signals occur at random intervals, but whenever they do, almost always they are repeated in a number of consecutive rows.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would like to only keep the 1st instance every time this happens, and remove the consecutive instances of the "buy" / "sell" word. Something like shown below –

enter image description here

>Solution :

Where the signal is equal to sell and the previous value row’s signal is sell, then assign none.

df.loc[df['signal'].eq('sell') & df['signal'].eq(df['signal'].shift()), 'signal'] = 'none'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading