Adding values in a column by "formula" in a pandas dataframe

Question:

I am trying to add values to a column using a formula, using the information from this question: Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply?

I already have the first number of the column B and I want to make a formula for the rest of column B.

The dataframe looks something like this:

A        B           C    
0.16     0.001433    25.775485
0.28     0           25.784443
0.28     0           25.792396
...    

And the method I tried was:

for i in range(1, len(df)):
    df.loc[i, "B"] = df.loc[i-1, "B"] + df.loc[i,"A"]*((df.loc[i,"C"]) - (df.loc[i-1,"C"]))

But this code produces an infinite loop, can someone help me with this?

Asked By: Francisco Barroca

||

Answers:

you can use shift and a simple assignment.

The general rule in pandas if you use loops you’re doing something wrong, it’s considered an anti pattern.

df['B_new'] = df['B'].shift(-1) - df['A'] * ((df['C'] - df['C'].shift(-1)))


      A         B          C     B_new
0  0.16  0.001433  25.775485  0.001433
1  0.28  0.000000  25.784443  0.002227
2  0.28  0.000000  25.792396       NaN
Answered By: Umar.H