Pedro Lobito
Pedro Lobito

Reputation: 98921

Compare values between different rows in Pandas

I'm new to pandas and I'm trying to compare values between rows without luck.
I want to find the RSI difference between the row where DOJI is 100 and next one.
In this case, 52.157595 - 45.430342.
What should I do for that?
I've played a bit with query() but I've no idea of what I'm doing...

                    Close   RSI       MOM   MACD        HAM  HMAN DOJI
20161125  10:13:00  69.87  52.157595 -0.21  0.011241    0     0   100
20161125  10:14:00  69.77  45.430342 -0.24  0.003785    0     0     0
20161125  10:15:00  69.77  45.430342 -0.10 -0.002099    0     0     0
20161125  10:16:00  69.83  49.924669 -0.02 -0.001899    0     0     0

Upvotes: 2

Views: 2828

Answers (3)

Brian
Brian

Reputation: 1645

you can also use shift in a similar way to diff

(df['RSI'] - df['RSI'].shift(-1))[df['DOJI'] == 100]

Upvotes: 0

user7184692
user7184692

Reputation:

I'd locate the rows and do manipulation in numpy

this_row = np.append((df.DOJI.values == 100)[:-1], False)
next_row = np.roll(this_row, 1)

rsi = df.RSI.values

pd.Series(rsi[this_row] - rsi[next_row],
          df.index.values[this_row], name='RSI')

Upvotes: 1

piRSquared
piRSquared

Reputation: 294258

try using diff with loc

df.RSI.diff(-1).loc[df.DOJI.eq(100)]

20161125  10:13:00    6.727253
Name: RSI, dtype: float64

explanation
Hopefully diff is self explanatory. However, the -1 aligns the index with the current row and not the next one. That means if I mask it with the boolean series of when DOJI is 100 then I have my answer.

Upvotes: 3

Related Questions