E
E
Elick2022-01-30 18:12:49
Python
Elick, 2022-01-30 18:12:49

How in Python, Pandas to do cumsum() on a column up to a specific sum value?

Hello, I have a question. There is a dataframe with numbers. You need to apply the cumsum() function to its column so that it gives the cumulative sum of this column, but the values ​​\u200b\u200bare added under a certain condition, after which the summation would start again. I'm trying to figure out how to do it without loops, so that it's as fast as possible, but maybe someone already knows the solution. Thanks in advance.

Note.
Condition: if the sum <= 6, the summation is in progress, otherwise the summation starts again
Input column ; Column on output
1 ; 1
3 ; 4
2 ; 65
; 5
3 ; 81
; 1
2 ; 3

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Elick, 2022-02-04
@Elick

The fastest thing that happened

vals = pd.Series([1,3,2,5,3,1,2])

fv = 6

def cumscums(vals, fv = fv):
    vals_ = []
    vals_.append(vals[0])  
    vals = vals[1:]
    for ind,line in enumerate(vals, start=1):
        if vals_[ind-1] < fv:
            vals_.append(vals_[ind-1] + line)
        else:
            vals_.append(line)
    return vals_

ccc = cumscums(vals, fv = fv)
pd.Series(ccc)

V
Viktor T2, 2022-01-31
@Viktor_T2

import pandas as pd

l = 

df = pd.DataFrame(l)
df['cumsum'] = df[1].cumsum()
df['group1'] = df[1].cumsum() // 6 # целочисленное деление как признак группы
df['group2'] = df[1] < 6

df['cum_6'] = df.groupby(['group1','group2'])[1].cumsum()

print(df)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question