-3

I have a data frame with column A and B. Desired outcome: If cumsum of B reaches value >=15, following operations for the rows between 0 and cumsum >=15 shall be computed: df["Amean"] =df["A"].mean() and df["Bsum15"] = df["B"].sum() ; then cumsum shall be reset to 0 again and the loop continues.

example

similar topic:
Python pandas cumsum() reset after hitting max

mqx
  • 11
  • 4
  • 1
    Why does the linked answer not answer your question, it seems to me to be an appropriate dupe – EdChum Jun 14 '17 at 11:06

1 Answers1

0

lets say, we take a simpler example

df=pd.DataFrame([1,2,3,4,5,6,7,8,9,10],columns=['B'])


def accum(vals):
    acc=0
    for i in vals:
        acc+=i
        if acc>=15:
           yield acc
           acc=0
        else:
           yield np.nan

df['accu']=list(accum(df['B'].values))

returns

    B   accu
0   1   NaN
1   2   NaN
2   3   NaN
3   4   NaN
4   5   15.0
5   6   NaN
6   7   NaN
7   8   21.0
8   9   NaN
9   10  19.0
suvy
  • 693
  • 6
  • 18