Answers for "cumulative sum in pandas"

2

cumulative sum in python

import pandas as pd
import numpy as np
  
# Create a dataframe
df1 = pd.DataFrame({"A":[None, 3, 8, 14], 
                   "B":[1, None, 4, 3], 
                   "C":[5, 3, 9,None]}) 
  
# Computing sum over Index axis
print(df1.cumsum(axis = 0, skipna = True))
#skipna exclude null values
Posted by: Guest on May-28-2021
0

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
Posted by: Guest on September-12-2020

Python Answers by Framework

Browse Popular Code Answers by Language