Answer the question
In order to leave comments, you need to log in
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
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question