I have a data frame which has column A consist of positive and negative number. I would like to make a column B which is the sum up to the current row of column A and start from the point value in A change sign just like below. And I have no clue where to start. Any help please?
A B
4 4
4 8
17 25
-1 -1
-8 -9
-9 -18
-45 -63
4 4
5 9
9 18
4 22
CodePudding user response:
Use groupby
and cumsum
df['B'] = df['A'].groupby(df['A'].lt(0).diff().cumsum().fillna(0)).cumsum()