python script for attribute table field calculator

2077
5
06-28-2017 04:43 PM
FaithWebster
Occasional Contributor

I'm using ArcMap 10.2. I have 2 columns in a joined table that I need to compare. They need to have the exact same content, and there are about 3,000 rows. I tried this: (changed field names for security)

[column1] ==!column3!
   return yes
else:
   return no

the Field calculation failed. I think I'm forgetting something really basic in the python script. I would appreciate any advice. 

Thank you!

0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Rather than fiddling around with a script...

1  Add a text/string field to your table... call it ... Check ... or something

2 Select by attribute ....  field1 == field2

3  make ... Check... the active field and open the field calculator and type 'yes' with the single quotes around the word

4  from the table, hit the switch selection button to get the reverse

5  from the Check field... type 'no'

you should now have all records filled ...Unless... you have Null values in your fields since a value won't compare to null.  If you have any of those, then get back

FaithWebster
Occasional Contributor

Thank you! That is a much easier solution! 

0 Kudos
FaithWebster
Occasional Contributor

Just a note: the select by attribute uses SQL and the expression was field 1=field 2. it worked perfectly!

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

besides what Dan said, always make sure you have a space on both sides of the ==      at least in your post it looks like the space is missing after the before the !    (which may just be a typo in the post)

BlakeTerhune
MVP Regular Contributor

When using an if statement, you probably have to use the field calculator code block option. You also mixed up the field name syntax between VB and Python.

code block:

def compareCalc(col1, col3):
    if col1 == col3:
       return 'yes'
    else:
       return 'no'‍‍‍‍‍

expression:

compareCalc(!column1!, !column3!)