0

I have this dataframe:

data={'col1':[1,3,3,1,2,3,2,2]}
df=pd.DataFrame(data,columns=['col1'])

I want to loop through it, and do something if a row as the same value as the last row.

Something like this:

for row, index in df.iterrows():
    if row == previous row: # that's the line I'm looking for
        print("ok")
  • do you want just for printing or are you storing the values? you can try `df.eq(df.shift())` if you want a series back – anky Jan 24 '21 at 15:00
  • No just printing. I don't need to store the value, just compare in a loop. –  Jan 24 '21 at 15:09
  • do the same as I suggested using `shift` : `s = df.eq(df.shift())` then `for _,r in s.iterrows(): if r.iat[0] : print('ok') else: print('not ok')` – anky Jan 24 '21 at 15:12

0 Answers0