简体   繁体   English

比较数据框中的两列,如果它们相等则产生 1 或 0

[英]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我想添加一个列来告诉我我的两个结果是否相同,这样我就可以计算 true/1 或 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) df_all['相同'] = df_all['ConvenienceStoreClosest'].apply(相同)

specific section from df_all df_all 的特定部分

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)
                  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM