Skip to content
Advertisement

Pandas – iloc – comparing value to the cell below

For the following table:

enter image description here

Using Pandas – I would like achieve the desired_output column, that is TRUE when the value below the current cell i different – otherwise FALSE.

I have tried the following code – but error occurs.

df['desired_output']=df.two.apply(lambda x: True if df.iloc[int(x),1]==df.iloc[int(x+1),1] else False)

Advertisement

Answer

df['desired_output'] = df['city'].shift().bfill() != df['city']
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement