Search code examples
pandascumsum

Can I create a reference index column that resets from 0 every time a cumsum threshold is reached


I am trying to add a cummulative sum column and an new index column n_index. Using exiting answers I have added a cumsum colum but the reference index column I have is not what I need.

df = pd.DataFrame({'amount':[4, 3, 7, 8, 2, 1, 5, 3, 5, 8]})

ls = []
n_index = []
cumsum = 0
last_reset = 0
threshold = 16

for i, row in df.iterrows():
    if cumsum + row.amount <= threshold:        
        cumsum = cumsum + row.amount
        n_index.append(i)
    else:        
        last_reset = cumsum        
        cumsum = row.amount
        n_index.append(0)
        
    ls.append(cumsum)    

df['cumsum'] = ls
df['n_index'] = n_index

The results is:

df
    

      amount    cumsum  n_index
    0   4   4   0
    1   3   7   1
    2   7   14  2
    3   8   8   0
    4   2   10  4
    5   1   11  5
    6   5   16  6
    7   3   3   0
    8   5   8   8
    9   8   16  9

I want to the dataframe n_index to start from zero (0) everytime the threshold is exceeded as below:

   amount   cumsum  n_index
0   4   4   0
1   3   7   1
2   7   14  2
3   8   8   0
4   2   10  1
5   1   11  2
6   5   16  3
7   3   3   0
8   5   8   1
9   8   16  2

Please help, Thank you.


Solution

  • Hope, you got your expected result, and remove the error.

    df = pd.DataFrame({'amount':[4, 3, 7, 8, 2, 1, 5, 3, 5, 8]})
    
    ls = []
    n_index = []
    cumsum = 0
    last_reset = 0
    threshold = 16
    
    assign_indx=0
    for i, row in df.iterrows():
        if cumsum + row.amount <= threshold:        
            cumsum = cumsum + row.amount
            n_index.append(assign_indx)
            assign_indx+=1
        else:        
            last_reset = cumsum        
            cumsum = row.amount
            n_index.append(0)
            assign_indx=1
        ls.append(cumsum)    
    
    df['cumsum'] = ls
    df['n_index'] = n_index
    
    #Output:
    
        amount  cumsum  n_index
    0   4   4   0
    1   3   7   1
    2   7   14  2
    3   8   8   0
    4   2   10  1
    5   1   11  2
    6   5   16  3
    7   3   3   0
    8   5   8   1
    9   8   16  2