I'm a new to pandas DataFrame, and I'm having a bit of struggle as I can't figure how to access a particular cell to make calculation to fill a new cell.
I'd like to use apply to call a external function with data from a cell at row - 1.
I did that, but outputing everything in a simple array, but i'm pretty sure there is a better way to do it:
I build my dataFrame from a csv with the following index:
DateIndex = pd.date_range(start="2005-1-1", end="2017-1-1", freq=BDay())
I'm positive my dataframe is ok, as per the extract below:
2005-01-03 0.005742
2005-01-04 0.003765
2005-01-05 -0.005536
2005-01-06 0.001500
2005-01-07 0.007471
2005-01-10 0.002108
2005-01-11 -0.003195
2005-01-12 -0.003076
2005-01-13 0.005416
2005-01-14 0.003090
So, I would like to add 100 to the first entry, and for the other ones, add one and then multiply it by the previous entry.
I was able to do so in an array:
for i in range(0,len(df.index)):
if i == 0:
listV = [df.iloc[i] + 100]
else:
listV.append(listV[i-1] * (1 + df.iloc[i]))
is there a way to do this and put the result directly in a new column of the data frame ?
Thanks a lot, Regards, Julien



df['new column name'] = listV. You need to remove the square brackets in your if statement otherwise it will turn the value into a list. The line should also be inside of an append statement as you had under your else statement.