how to calculate cumulative sum in python code example
Example 1: numpy cumulative distribution function normal
>>> vals = norm.ppf([0.001, 0.5, 0.999])
>>> np.allclose([0.001, 0.5, 0.999], norm.cdf(vals))
True
Example 2: pandas cumulative sum column
import pandas as pd
from random import randint
df = pd.DataFrame(data=[{'duration': randint(0,3)} for _ in range(5)])
df.head()
# duration
# 0 0
# 1 2
# 2 1
# 3 0
# 4 3
df['cum_dur'] = df.duration.cumsum()
df.head()
# duration cum_dur
# 0 0 0
# 1 2 2
# 2 1 3
# 3 0 3
# 4 3 6