Reputation: 2273
I am looking forward to apply a function that involves 2 dfs:
df1: A B C D E
12/2/2001 3 4 2 3 4
12/3/2001 5 5 5 4 6
12/4/2001 9 8 7 1 1
df_new = pd.DataFrame().reindex_like(df1)
df_new.loc[df_new.index[0]-pd.offsets.DateOffset(days=1)]=0
df_nuevo=df_new.sort_index()
for i in range(1,len(df_nuevo)):
row=((df1.iloc[:i])*0.55)*((df_nuevo.iloc[:i-1])*0.45)
df_nuevo.append(row)
print(df_nuevo)
The output I am expecting is df_nuevo
filled with the appended rows. Actually gets filled by NaNs. Can anyone help ? Thank you .
This is the current output :
A B C D E
12/1/2001 0 0 0 0 0
12/2/2001 NaN NaN NaN NaN NaN
12/3/2001 NaN NaN NaN NaN NaN
12/4/2001 NaN NaN NaN NaN NaN
The idea is that where there are the NaNs appears the results of the formula specified in the part of the code : rows
Upvotes: 1
Views: 79
Reputation: 22463
If you're just trying to iteratively fill out rows of df2
based on df1
and prior values of df2
, a simple recasting of your formula (at least the way you originally gave it in pseudo-code) would be:
# create all-zeros df2 same shape as df1
df2 = df1.copy()
df2.loc[:,:] = 0
# iteratively compute df2
for i in range(len(df1)):
df2.iloc[i] = df1.iloc[i] * .55 + df2.iloc[i-1] * .45
For the given example above, the results for df2
would be:
It may be instructive to see the values as they are updated through the loop:
i: 0
df1.iloc[i].values: array([ 3., 4., 2., 3., 4.])
df2.iloc[i - 1].values: array([ 0., 0., 0., 0., 0.])
resulting df2.iloc[i].values: array([ 1.65, 2.2 , 1.1 , 1.65, 2.2 ])
i: 1
df1.iloc[i].values: array([ 5., 5., 5., 4., 6.])
df2.iloc[i - 1].values: array([ 1.65, 2.2 , 1.1 , 1.65, 2.2 ])
resulting df2.iloc[i].values: array([ 3.4925, 3.74 , 3.245 , 2.9425, 4.29 ])
i: 2
df1.iloc[i].values: array([ 9., 8., 7., 1., 1.])
df2.iloc[i - 1].values: array([ 3.4925, 3.74 , 3.245 , 2.9425, 4.29 ])
resulting df2.iloc[i].values: array([ 6.521625, 6.083 , 5.31025 , 1.874125, 2.4805 ])
Upvotes: 1
Reputation: 38415
Is this what you are looking for?
df1 = (df* 0.55 )+ (0.45 * df.shift())
A B C D E
2001-12-02 NaN NaN NaN NaN NaN
2001-12-03 4.1 4.55 3.65 3.55 5.10
2001-12-04 7.2 6.65 6.10 2.35 3.25
Upvotes: 0