0

I have the following DataFrame:

            AAPL shares  GOOG shares  MSFT shares
date                                             
2019-01-01          NaN         10.0          NaN
2019-01-05          NaN          NaN         15.0
2019-01-12          NaN          NaN          7.0
2019-01-13          3.0          NaN          NaN
2019-01-14          NaN         -5.0          NaN

After applying a forward fill:

print(df.set_index('date').sort_index().fillna(method='ffill').fillna(value=0))

I get:

            AAPL shares  GOOG shares  MSFT shares
date                                             
2019-01-01          0.0         10.0          0.0
2019-01-05          0.0         10.0         15.0
2019-01-12          0.0         10.0          7.0
2019-01-13          3.0         10.0          7.0
2019-01-14          3.0         -5.0          7.0

My question is there any way to fill forward with simple addition? The result I'm looking for:

            AAPL shares  GOOG shares  MSFT shares
date                                             
2019-01-01          0.0         10.0          0.0
2019-01-05          0.0         10.0         15.0
2019-01-12          0.0         10.0         22.0
2019-01-13          3.0         10.0         22.0
2019-01-14          3.0          5.0         22.0
Sterling Butters
  • 1,024
  • 3
  • 20
  • 41

1 Answers1

1

You can check with cumsum

df.fillna(0).cumsum()
            AAPLshares  GOOGshares  MSFTshares
date                                          
2019-01-01         0.0        10.0         0.0
2019-01-05         0.0        10.0        15.0
2019-01-12         0.0        10.0        22.0
2019-01-13         3.0        10.0        22.0
2019-01-14         3.0         5.0        22.0
BENY
  • 317,841
  • 20
  • 164
  • 234