Home > front end >  Pandas Cummax starting with x row
Pandas Cummax starting with x row

Time:02-27

I want to have cummax start at x index Psuedocode would be:

   df['highest']= df['somenumber'](starting at index x).cummax()
   df['somenumber'] is 10,50,30,20,80,5
   x=2 
   df['highest'] would be 0,0,30,30,80,80

CodePudding user response:

use shift twice:

x=2
df['highest']= df['somenumber'].shift(-x).cummax().shift(x).fillna(0)

output:


    somenumber  highest
0   10          0.0
1   50          0.0
2   30          30.0
3   20          30.0
4   80          80.0
5   5           80.0
  • Related