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.
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