compare two fields if they 'are equal or not, in the field calculator

2742
4
08-20-2014 03:56 PM
GustavoCordero
New Contributor III

I have two "A" fields and "B" and I want to use the field calculator in the "C" field to determine if they are equal

put 1 if they are equal and 0 if they  not

  example

 

 

    A | B | C |

     3 | 3 | 1 |

     5 | 7 | 0 |

 

 

I found this scrip

but it only works in python window

 

 

import arcpy

 

 

fc = R 'c: \ path \ to \ your \ fc'

 

 

with arcpy.da.UpdateCursor (fc, ["FIELD1", "FIELD2", "FIELD3"]) as cursor:

     for row in cursor:

         if row [0] == row [1]:

             row [2] = 1

         else:

             row [2] = 0

         cursor.updateRow (row)

 

 

"I want to use the field calculator"

  or as a tool for modelbuidel where "fc = R'c: \ path \ to \ your \ fc '         can be a Shapefile imput

0 Kudos
4 Replies
RiyasDeen
Regular Contributor

Hi Gustavo,

Try below:

Untitled.png

Script:

def isEqual(x,y):

if x == y:

  return 1

else:

  return 0

DarrenWiens2
MVP Honored Contributor

Or in VBScript:

Capture.JPG

GustavoCordero
New Contributor III

thank you very much for your answer

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Of course both meant to point out that you would replace their field names with yours...in the case of python you need to remember that field names are enclosed in ! marks whereas in VBscript they are surrounded by square brackets.  Also, in python, the equality check is == whereas in VBscript a single = will suffice.

0 Kudos