I would like to do the same in python pandas as shown on the picture. enter image description here
This is sum function where the first cell is fixed and the formula calculates "continuous sum".
I tried to create pandas data frame however I did not manage to do this exactly.
CodePudding user response:
You can use DataFrame.cumsum()
to achieve what you want:
import pandas as pd
df = pd.DataFrame([10, 20, 30])
print(df.cumsum())
CodePudding user response:
Based on the example you shared, use pandas.Series.cumsum
with pandas.Series.ffill
:
import pandas as pd
import numpy as np
df= pd.DataFrame({'Nuber': [10.0, np.NaN, 20.0, np.NaN, np.NaN, 10.0, np.NaN, np.NaN, 30.0]})
df["Sum cont"]= df["Nuber"].cumsum().ffill()
# Output :
print(df)
Nuber Sum cont
0 10.0 10.0
1 NaN 10.0
2 20.0 30.0
3 NaN 30.0
4 NaN 30.0
5 10.0 40.0
6 NaN 40.0
7 NaN 40.0
8 30.0 70.0
CodePudding user response:
I would refer to pandas cumsum() For example:
df['NEW_COLUMN_CUMULATED'] = df['OLD_COLUMN'].cumsum()