Consider below dataFrame. I want to calculate if the current value of price column is greater than last 10 values. I was thinking to use shift, but not sure how to use for last 10 rows.
price
220 3.337
221 3.320
222 3.290
223 3.291
224 3.312
225 3.255
226 3.216
227 3.245
228 3.275
229 3.282
230 3.370
231 3.396
232 3.375
233 3.369
234 3.335
235 3.344
236 3.365
237 3.373
238 3.414
239 3.378
Output dataframe:
price isGreater
220 3.337 NaN
221 3.320 NaN
222 3.290 NaN
223 3.291 NaN
224 3.312 NaN
225 3.255 NaN
226 3.216 NaN
227 3.245 NaN
228 3.275 NaN
229 3.282 NaN
230 3.370 1.0
231 3.396 1.0
232 3.375 NaN
233 3.369 NaN
234 3.335 NaN
235 3.344 NaN
236 3.365 NaN
237 3.373 NaN
238 3.414 1.0
239 3.378 NaN
CodePudding user response:
You can use rolling
max
to get the max of the last 10 rows, if greater than it, then it's greater or equal than all (including self, thus the 1):
df['isGreater'] = df['price'].ge(df['price'].rolling(10 1).max())
NB. technically if you really want to compare only to the previous rows and not self (for example to use strict comparison), you would need to shift:
df['isGreater'] = df['price'].gt(df['price'].shift().rolling(10).max())
output:
price isGreater
220 3.337 False
221 3.320 False
222 3.290 False
223 3.291 False
224 3.312 False
225 3.255 False
226 3.216 False
227 3.245 False
228 3.275 False
229 3.282 False
230 3.370 True
231 3.396 True
232 3.375 False
233 3.369 False
234 3.335 False
235 3.344 False
236 3.365 False
237 3.373 False
238 3.414 True
239 3.378 False