Home > Software design >  Do rolling on all dataframe rows
Do rolling on all dataframe rows

Time:02-08

I would like to find in dataframe of prices all all-time highs in that were in history.

timestamp close ath
x 1234 0
x 2000 1
x 1956 0
x 1884 0
x 2234 1

So for specific row I would like to find a maximum for all previous rows and check if value in current row is greater than this maximum. I cannot do it without indexing for all rows and I know that it is inefficient.

CodePudding user response:

Try this:

df['ath'] = df.groupby('timestamp')['close'].shift(1).lt(df['close']).astype(int)

Output:

>>> df
  timestamp  close  ath
0         x   1234    0
1         x   2000    1
2         x   1956    0
3         x   1884    0
4         x   2234    1

CodePudding user response:

FWIW, here is an alternative:

df['ath'] = (df['close'] > df['close'].expanding().max().shift()).astype(int) 

CodePudding user response:

Use cummax to keep the highest seen value then shift rows and compare to the current row:

df['ath'] = df['close'].gt(df['close'].cummax().shift()).astype(int)
print(df)

# Output
  timestamp  close  ath
0         x   1234    0
1         x   2000    1
2         x   1956    0
3         x   1884    0
4         x   2234    1
  •  Tags:  
  • Related