Alissa L.
Alissa L.

Reputation: 103

How to sum values of pandas dataframe by rows?

I have a data frame like this:

A 
0
1
0
2

and I would like to sum the values "so far" of the dataframe in a cumulative format, so if A increases by 1 then I would like the sum to increase by 1 as well, as so:

A   Sum
0   0
1   1
0   1
2   2

I have to keep a record of when this change occurs for the analysis, so I can't just sum the entire column at once.

I thought about doing:

df = df.assign(A_before=df.A.shift(1))
df['change'] = (df.A - df.A_before)
df['sum'] = df['A'] + df['A_before'] 

but it's not adding the sum values from the previous rows as well, only the values in the same rows.

Any solutions? Thank you.

Upvotes: 1

Views: 85

Answers (1)

BENY
BENY

Reputation: 323226

You can do diff with cumsum

df.A.diff().ge(1).cumsum()
0    0
1    1
2    1
3    2
Name: A, dtype: int64
df['sum']=df.A.diff().ge(1).cumsum()

Upvotes: 3

Related Questions