2

I want to do the following calculation and the outcome has to be a new column Calculated trap..

test["calculation trap"] = (( 0.000164  + 0.000415)/2)

so the outcome of this formula has to be 0.0002895.

I tried the following code to do this calculation for the whole column, but i got the outcome in the column below.

test["calculation trap"] = ((test["calculation"][0:]+test["calculation"][1:])/2).reset_index(drop=True)
    Temp    calculation.    calculation trap.
0   90.01   0.000164        NaN
1   91.03   0.000415        0.000415
2   95.06   0.001315        0.001315
3   100.07  0.002896        0.002896
4   103.50  NaN             NaN

csymvoul
  • 677
  • 3
  • 15
  • 30
RvBenthem
  • 35
  • 4

1 Answers1

1

Use Series.shift with -1:

test["calculation trap"] = ((test["calculation"].shift(-1)+test["calculation"])/2)
print (test)
     Temp  calculation  calculation trap
0   90.01     0.000164          0.000290
1   91.03     0.000415          0.000865
2   95.06     0.001315          0.002106
3  100.07     0.002896               NaN
4  103.50          NaN               NaN
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thanks!. The outcome "0.00290" has to be placed on index 0. and the outcome " 0.000865" on index 1 etc. – RvBenthem Apr 28 '20 at 08:02