lmiguelvargasf
lmiguelvargasf

Reputation: 70003

pandas: cumsum ignoring first two rows

I have a dataframe which has the following column:

|---------------------| 
|          A          |
|---------------------|
|           0         |
|---------------------|
|         2.63        |
|---------------------|
|         7.10        |
|---------------------|
|         5.70        |
|---------------------|
|         6.96        |
|---------------------|
|         7.58        |
|---------------------|
|         3.3         |
|---------------------|
|         1.93        |
|---------------------|

I need to get the cumulative sum, but the point is kind of particular. The first element should be 0, and the following are the cumulative sum starting from the previous column, so in this case I need to produce:

|---------------------| 
|          B          |
|---------------------|
|           0         |
|---------------------|
|           0         |
|---------------------|
|         2.63        |
|---------------------|
|         9.73        |
|---------------------|
|        15.43        |
|---------------------|
|        22.39        |
|---------------------|
|        29.97        |
|---------------------|
|        33.27        |
|---------------------|

I know that it is easily achieve when not having the condition I am asking for by:

df['B'] = df.A.cumsum()

However, I don't have any idea how to solve this issue, and I was thinking to implement a for loop, but I hope there is a simply way using pandas.

Upvotes: 3

Views: 2983

Answers (2)

fkoh111
fkoh111

Reputation: 1

Building atop of the answer from @jezrael: you can disregard fillna() and do:

df = pd.DataFrame({"A": [0, 2.63, 7.10, 5.70, 6.96, 7.58, 3.3, 1.93]})

df = df.A.cumsum().shift(fill_value=0)

print (df)

0     0.00
1     0.00
2     2.63
3     9.73
4    15.43
5    22.39
6    29.97
7    33.27
Name: A, dtype: float64

Upvotes: 0

jezrael
jezrael

Reputation: 863801

You can add shift and fillna:

df = df.A.cumsum().shift().fillna(0)
print (df)
0     0.00
1     0.00
2     2.63
3     9.73
4    15.43
5    22.39
6    29.97
7    33.27
Name: A, dtype: float64

Upvotes: 6

Related Questions