I am trying to compute the column values of a pandas data frame "recursively".
Suppose there is data for two different days, each of which has 10 observations, and you want to calculate some variable r, where only the first value of r is given (every day), and you want to calculate the remaining 2 * 9 records, while how each subsequent value depends on the previous record r and one additional "simultaneous" variable "x".

The first problem is that I want to perform calculations for each day separately, that is, I would like to use the function pandas.groupby()for all my calculations ... but when I try to multiply the data and use it shift(1), I get only "NaN" records
data.groupby(data.index)['r'] = ( (1+data.groupby(data.index)['x']*0.25) * (1+data.groupby(data.index)['r'].shift(1)))
for ():
for i in range(2,21):
data[data['rank'] == i]['r'] = ( (1+data[data['rank'] == i]['x']*0.25) * (1+data[data['rank'] == i]['r'].shift(1))
. DataFrames? , - ?
:
df = pd.DataFrame({
'rank' : [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10],
'x' : [0.00275,0.00285,0.0031,0.0036,0.0043,0.0052,0.0063,0.00755,0.00895,0.0105,0.0027,0.00285,0.0031,0.00355,0.00425,0.0051,0.00615,0.00735,0.00875,0.0103],
'r' : [0.00158,'NaN','NaN','NaN','NaN','NaN','NaN','NaN','NaN','NaN',0.001485,'NaN','NaN','NaN','NaN','NaN','NaN','NaN','NaN','NaN']
},index=['2014-01-02', '2014-01-02', '2014-01-02', '2014-01-02',
'2014-01-02', '2014-01-02', '2014-01-02', '2014-01-02',
'2014-01-02', '2014-01-02', '2014-01-03', '2014-01-03',
'2014-01-03', '2014-01-03', '2014-01-03', '2014-01-03',
'2014-01-03', '2014-01-03', '2014-01-03', '2014-01-03'])