Home > front end >  Reversed Cumulative sum in python - Assigning 0 to the bottom
Reversed Cumulative sum in python - Assigning 0 to the bottom

Time:01-29

I have wrote a python code but now stucked at getting 0 value to the bottom of the column. Pls refer the code I wrote

df2['cumsum_reverse'] = df2.loc[::-1, 'OS for the month'].cumsum()[::-1]

Below is the output table

Click here for the table

In the end, I have added the column that need to come.

Can you help me for this issue?

CodePudding user response:

IIUC, use pandas.Series.shift :

df2['cumsum_reverse'] = df2.loc[::-1, 'OS for the month'].shift().cumsum()[::-1].fillna("-")

Output :

print(df2['cumsum_reverse'])

2022-01     1610049.2
2022-02     1118224.93
2022-03     967592.69
2022-04             -
Name: cumsum_reverse, dtype: object
  • Related