I have a dataframe df with two columns colA and colB. and want to perform a calculation using the values of both columns and asign it to the next row in colA so that value calculated at the previous row will be used in the next calculation. Of course at the last row of colA no calculation should be performed as it would try to assign a value out of colA.
df = pd.DataFrame({'colA' :[1, None, None, None, None, None], 'colB' : [4, 5, 1, 3, 8, 9]})
the formula to be used is
x = (colA + colB) / 2
Instead of assigning a value to the next row of colA an alternative could be using data from the previous row while performing the calculation.
The picture shows the final result
In Excel this is easy but how to do this in Python?
Update question: Of course this can be done using a for loop, but I would like to do this without a For-loop in a one liner in order to vectorize it