Aug-21-2017, 02:43 AM
It is easy to add a new column to store the results of calculation on the same row as below.
I need to add a column df["d"] that will contain the results of calculation with current row i and next row i+1
something like: df["d"][i] = (df["b"][i] - df["a"][i+1]) / df["a"][i+ 1]
I can use "for loop" to do that but wondering if another better way. thanks
I need to add a column df["d"] that will contain the results of calculation with current row i and next row i+1
something like: df["d"][i] = (df["b"][i] - df["a"][i+1]) / df["a"][i+ 1]
I can use "for loop" to do that but wondering if another better way. thanks
import pandas as pd df = pd.DataFrame({"a": [11,12,13,14,15,16], "b": [15,20,30,20,30,50]}) df["c"] = (df["b"] - df["a"]) / df["a"] print(df)