English

How To: Compare Row Values in Two Fields

Summary


Warning:
The procedure below is for ArcGIS 10.0 or later versions. Python scripts are not a supported expression type in earlier versions.

Instructions provided demonstrate how to use the Field Calculator to compare two fields on the same row of data. Fields that are different will be flagged with 0. Fields that are the same will be flagged with a 1.

Procedure

  1. Create a new field. Set the type as short or long integer and accept the other defaults.

    A. If editing, stop the current edit session.
    B. Open the feature class attribute table.
    C. Click the Table Options button.
    D. Select 'Add Field' to open the Add Field dialog box.
    E. Name the new field.
    F. Select 'Long Integer' from the 'Type' drop-down list.
    G. Accept the default values for all Field Properties.
    H. Click OK.

  2. Right-click the newly created field in the step above and click "Field Calculator."
  3. Select the Python parser.
    [O-Image]
  4. Ensure that the 'Show Codeblock' option is checked.
  5. Paste the following code into the Pre-Logic Script Code box:
    .
    Code:
    def isSame(x, y):
    fieldA = str(x)
    fieldB = str(y)
    if fieldA == fieldB:
    return 1
    else:
    return 0

  6. Type 'isSame(!FieldA!, !FieldB!)' in the lower expression box and replace the words FieldA and FieldB with the names of the fields you want to compare.

    The results of Steps 4 - 6 should resemble the following:
    [O-Image]
  7. Click OK. Fields that are the same are designated with a value of 1 and fields are different are designated with a value of 0 in the newly added field.