Home > database >  How do I begin calculating row values at a specific index?
How do I begin calculating row values at a specific index?

Time:02-27

I'm working with a data set where a column value is dependent on the value of the row (same column) right above it. because this calculation needs a certain amount of datapoints to begin, I cannot start at index 0. Here is a sample of the data set with the initial value calculated already (col Net Change Avg):

     Close  Daily Returns  Net Change Avg
0   353.74           0.29             NaN
1   354.18           0.44             NaN
2   352.16          -2.02             NaN
3   354.57           2.41             NaN
4   354.08          -0.49             NaN
5   353.92          -0.16             NaN
6   353.55          -0.37             NaN
7   354.87           1.32             NaN
8   354.14          -0.73             NaN
9   351.44          -2.70             NaN
10  350.73          -0.71             NaN
11  349.31          -1.42             NaN
12  346.70          -2.61             NaN
13  349.30           2.60             NaN
14  346.47          -2.83             NaN
15  348.82           2.35       -0.382857
16  348.18          -0.64             NaN
17  345.64          -2.54             NaN
18  339.40          -6.24             NaN
19  339.11          -0.29             NaN

I need to use the Net Change Avg at index 15 to compute a value and store it in index 16, and then use the stored value in index 16 to compute a value to store in index 17, and so on. This is what I tried, but it seems to only populate index 16 and doesn't propagate down the column.

dia.loc[16:, 'Net Change Avg'] = dia['Net Change Avg'].shift(1)   1

I'm new here, please forgive any formatting or convention errors I've made.

CodePudding user response:

for i in range(16,len(df)):
    df.loc[i,'Net Change Avg']=df.loc[i-1,'Net Change Avg']  

It was not clear that what value you are trying to insert at index 16. I just run a loop it will copy the value at index 15 and continue till length of dataframe

  • Related