RvBenthem
RvBenthem

Reputation: 35

Dataframe calculation

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

Upvotes: 2

Views: 50

Answers (1)

jezrael
jezrael

Reputation: 862601

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

Upvotes: 1

Related Questions