Pandas comparing values following each other

Question:

So the problem is to check are these values inside the same column are equal to each other
i.e.

df = pd.DataFrame(np.array([[1, 2, 3], [1, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])

And if the following values of ‘a’ column are equal is there any chance to change the value of ‘c’ column for the same(or any other) row.
Seems like pandas doesn’t like cycles for…

for i in range(len(df)-1):
   if df['a'][i+1]==df['a'][i]: 
      df['c'][i]='XXX'` 

but doesn’t work in pandas, just need to compare n value with n+1 value and change value in another column

Asked By: Netbek

||

Answers:

Since it’s a dataframe, you could use duplicated

df.loc[df.duplicated('a', keep=False), 'c']='XXX'

I mean, if your goal was to set column ‘c’ to ‘XXX’ for every row whose column ‘a’ is equal to value of column ‘a’ of another row.

It seems to be what you described. But your code attempt looks as if you were in fact trying to find only subsequent identical values.

In which case you are looking for

df.loc[df.a.shift(-1)==df.a, 'c']='XXX'
Answered By: chrslg