Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

specific section from df_all

>Solution :

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)
                  )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading