Reputation: 1175
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
Upvotes: 0
Views: 90
Reputation: 323306
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
Upvotes: 1