2

I have a data frame (df) with different "price"s and I want to compare these prices and make a decision.

df['Decision'] = np.where((df['price1'] > df['price2']) ,'sell',np.where((df['price1'] < df['price2']),'buy',np.nan))

My output is:

price1 price2 Decision
50 50 NaN
100 200 buy
70 140 buy
150 200 buy
150 50 sell
60 20 sell

But I want to have just the "first signal" of "buy" or "sell" and delete replication until the next signal, as:

price1 price2 Decision
50 50 NaN
100 200 buy
70 140
150 200
150 50 sell
60 20
SA12
  • 318
  • 2
  • 10

1 Answers1

2

You can try with idxmax:

df.loc[(df['price1'] > df['price2']).idxmax(), 'Decision'] = 'sell'
df.loc[(df['price1'] < df['price2']).idxmax(), 'Decision'] = 'buy'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Nk03
  • 14,699
  • 2
  • 8
  • 22
  • It gets the first table, not the second. I don't want to have the same signals until the decision changing. – SA12 May 31 '21 at 05:38