I have panda series as the following : 1 1 2 2 3 3 4 4 5 0 6 0 7 1 8 2 9 3 10 0 11 0 12 0 13 0 14 1 15 2 I have to arrange this in following format : 1 1 2 2 3 3 4 4 5 0 6 0 7 3 ---> 4-2+1 (previous non zero value - amount of previous zeroes + current value) 8 4 ---> 4-2+2 (previous non zero value - amount of previous zeroes + current value) 9 5 ---> 4-2+3 (previous non zero value - amount of previous zeroes + current value) 10 0 11 0 12 0 13 0 14 2 ---> 5-4+1 (previous non zero value - amount of previous zeroes + current value) 15 3 ---> 5-4+2 (previous non zero value - amount of previous zeroes + current value) I am stuck at this. Till now I am able to produce a data frame with consecutive zeroes. zero = ser.eq(0).groupby(ser.ne(0).cumsum()).cumsum() which gave me: 1 0 2 0 3 0 4 0 5 1 6 2 7 0 8 0 9 0 10 1 11 2 12 3 13 4 14 0 15 0 if someone willing to assist on this. i am dropping cookie cutter for this problem which will create the above series. d = {'1': 1, '2': 2, '3': 3, '4':4, '5':0, '6':0, '7':1, '8':2, '9':3, '10':0, '11':0, '12':0, '13':0, '14':1, '15':2} ser = pd.Series(data=d) Continue reading...