Reputation: 1813
In order to calculate the 'RS' column. Implemented a for loop in python to calculate the 'RS' column. Please comment how to do that in pandas correctly.
Expected result
Date Adj Close Change RS
2017-11-30 253.94 NaN 100.00
2017-12-01 253.41 -0.208711 99.791289
2017-12-04 253.11 -0.118385 99.672904
2017-12-05 252.19 -0.363478 99.309426
2017-12-06 252.24 0.019826 99.329252
Incorrect statement
baselinedf['RS'] = baselinedf['Change'] + baselinedf['RS'].shift(-1)
Correct but inappropriate
baseValue=100
x=0
for i in baselinedf.index:
if x>0 :
baseValue=baselinedf.loc[i,'Change']+baseValue
baselinedf.loc[i,'RS']=baseValue
x = x + 1
Upvotes: 0
Views: 47
Reputation: 22503
IIUC you are looking for cumsum
:
df["new"] = 100+df["Change"].fillna(0).cumsum()
print (df)
Date Adj Close Change RS new
0 2017-11-30 253.94 NaN 100.000000 100.000000
1 2017-12-01 253.41 -0.208711 99.791289 99.791289
2 2017-12-04 253.11 -0.118385 99.672904 99.672904
3 2017-12-05 252.19 -0.363478 99.309426 99.309426
4 2017-12-06 252.24 0.019826 99.329252 99.329252
Upvotes: 3