In my code I assign a value to a cell in a data frame, based on another value in the same data frame but in another row.
The code, using a for-loop is as follows:
df = pd.DataFrame({'A':[1, 2, 3],'B':[4, 5, 6]})
for i in range(1, df.shape[0]):
df.loc[i, 'C'] = df.loc[i-1, 'B']
Output:
A B C
0 1 4 NaN
1 2 5 4.0
2 3 6 5.0
This code gives me the output I want, but the code is rather slow. I read about df.itterrows
and df.apply
but I cannot find out how this can work for my code since I refer to other rows. Does anyone know a faster way to iterate over rows, referring to other rows in the pandas data frame?