0

I am looping over a pandas.DataFrame (df). How can I compare two values in two neighboring rows? I would like, if possible, to use iterrows.

Example code for demonstration:

for index, row in df.iterrows():
    row0 = row[0] # take value of column 0 of row
    # jump to row1 - how?
    row1 = row1[0] # take value of column 0 of next row
    if row1 > row0: # compare the two values
        # do something
len
  • 588
  • 1
  • 5
  • 19
  • Don't use `iterrows`, use `shift` for vector operations. If you **really** need to loop, just store the value of the given row to reference in the next step and start iterating from the second row – mozway Feb 11 '22 at 15:03

0 Answers0