Skip to content Skip to sidebar Skip to footer

Python Pandas Get A Cumulative Sum (cumsum) Which Excludes The Current Row

I am trying to get a cumulative count of a given column that excludes the current row in the dataframe. My code is shown below. The problem with using cumsum() only is that it inc

Solution 1:

what about this one?

df['ExAnte Good Year Count'] = df['Good Year'].shift().cumsum()

The result should be the following:

YearGoodYearExAnteGoodYearCount02000          1NaN12001          01.022002          11.032003          02.042004          02.052005          12.062006          13.072007          14.082008          05.0

Solution 2:

df['Yourcol']=df.groupby('YearType',sort=False)['GoodYear'].apply(lambdax :x.shift().cumsum())dfOut[283]:GoodYearYearYearTypeYourcol012000         XNaN102001         YNaN212002         ZNaN302003         Z1.0402004         Z1.0512005         X1.0612006         Y0.0712007         Z1.0802008         Z2.0

Post a Comment for "Python Pandas Get A Cumulative Sum (cumsum) Which Excludes The Current Row"