Home > Enterprise >  Looping through a dataframe (sorry) and checking if the current value exist in the previously looped
Looping through a dataframe (sorry) and checking if the current value exist in the previously looped

Time:10-23

I'm looping through a dataframe (sorry, I know that's not ideal) and I wanted to check (at a certain point, for example i=n) if the current value on the column 'SKU', is present in the previous (n-1) values, so I tried:

if df['SKU'][i] not in df['SKU'][:i-1]:

but it's not working. Any help is greatly appreciated

CodePudding user response:

>>> df.SKU == df.SKU.shift()

CodePudding user response:

By way of trial and error, I figured I just needed to use .values at the end of the comparison like so:

if df['SKU'][i] not in df['SKU'][:i-1].values:

Thanks!

  • Related