0

In pandas dataframe, is it possible to calculate rows which need previous value without actual iterate each row? For example:

        for i in range(1, len(price)):
            if price.index[i] in trade_date_index_list:
                gross_pnl.iat[i, 0] = price.iloc[i]['A'] + price.iloc[i]['B']
            else:
                gross_pnl.iat[i, 0] = gross_pnl.iat[i - 1, 0] + price.iloc[i]['C']

Is there a way to avoid iterating each row? I think the tricky part is that to calculate the next row, I need to have the value in current row which is determined by the if condition. Thank you.

Xiao Tan
  • 63
  • 4

0 Answers0