I have a pandas dataframe in python.
I need to iterate over each column and calculate a value and based in this value I have to calculate the next row value.
Right now I am doing it using iterrows():
value = 1000
df['calculated_column'] = 0
for index, row in df.iterrows():
df.loc[index,'calculated_column'] = (df.loc[index -1 ,'calculated_column'] - df.loc[index,'column_to_sum']) if index != 0 else value
So, it is going to be something like this:
row 1 => df['calculated_column'] = 1000
row 2 => df['calculated_column'] = 1000 + df['column_to_sum'] = 1100
row 3 => df['calculated_column'] = 1100 + df['column_to_sum'] = 1200
I have read that do iterrows to iterate over a pandas dataframe should be avoided: How to iterate over rows in a DataFrame in Pandas
How could I do this process without iterrows? I have tried doing it with the apply function but I don't know how to use it