Home > Software design >  compare two columns in data frame, then produce 1 or 0 if they are equal or not
compare two columns in data frame, then produce 1 or 0 if they are equal or not

Time:11-15

I wanted to add a column that would tell me if two of my results were the same so I could calculate a % of true/1 or false/0

def same(closests):
    if 'ConvenienceStoreClosest' >= 'ConvenienceStoreClosestOSRM':
        return 1
    else:
        return 0

This is what I tried

df_all['same'] = df_all['ConvenienceStoreClosest'].apply(same)

specific section from df_all

CodePudding user response:

Never use a loop/apply when you can use vectorial code.

In your case a simple way would be:

df_all['same'] = (df_all['ConvenienceStoreClosest']
                  .eq(df['ConvenienceStoreClosestOSRM'])
                  .astype(int)
                  )
  • Related