Abhis
Abhis

Reputation: 605

Replicating excel calculation in python dataframe

Hi I am trying to replicate this excel calculation =C2*A3^B3 in python dataframe. But not able to get the calculated field of the previous row into my calculation.

Excel data

formuls

Input Data :

 ftax        stax
 1.000000000     0   
 0.999959316     3   
 0.999959316     1   
 0.999959316     3   
 0.999959316     3   
 0.999959316     1

If I put the Values in excel and apply this formula I get below result and wanted to replicate the same in python

=C2*A3^B3 in cell C3 and =A2^B2in Cell C2

Desired Output :

ftax         stax   cal_field
1.000000000  0      1.000000000
0.999959316  3      0.999877952
0.999959316  1      0.999837272
0.999959316  3      0.999715244
0.999959316  3      0.999593230
0.999959316  1      0.999552562

Sample code which I tried

nav_df = pd.DataFrame(nav_cal)
nav_df[cal_filed_1] = nav_df[cal_filed].shift(1).fillna("1")

print (nav_df[cal_filed]*(nav_df[cal_filed_1])**nav_df[9]) #I think this is not the right way

And I am getting below Output:

      ftax     stax cal_field
0     0.999996   0  1.000000  
1     0.999996   3  0.999837  
2     0.999996   1  0.999919  
3     0.999996   3  0.999837  
4     0.999996   3  0.999837  
5     0.999996   1  0.999919  

Upvotes: 2

Views: 308

Answers (1)

jezrael
jezrael

Reputation: 862511

Need cumprod, only is necessary first value is always 1:

df['cal_field'] = (df['ftax'] ** df['stax']).cumprod()
print (df)
       ftax  stax  cal_field
0  1.000000     0   1.000000
1  0.999959     3   0.999877
2  0.999959     1   0.999836
3  0.999959     3   0.999713
4  0.999959     3   0.999590
5  0.999959     1   0.999549

Upvotes: 5

Related Questions