How To: Compare row values in two fields
Note: 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.
- Create a new field. Set the type as short or long integer and accept the other defaults.
- If editing, stop the current edit session.
- Open the feature class attribute table.
- Click the Table Options button.
- Select Add Field to open the Add Field dialog box.
- Name the new field.
- Select Long Integer from the Type drop-down list.
- Accept the default values for all Field Properties.
- Click OK.
- Right-click the newly created field in the step above and click Field Calculator.
- Select the Python parser.
- Ensure that the Show Codeblock option is checked.
- Paste the following code into the Pre-Logic Script Code box:
def isSame(x, y): fieldA = str(x) fieldB = str(y) if fieldA == fieldB: return 1 else: return 0
- 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:
- 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.
Last Published: 9/23/2019
Article ID: 000011200
Software: ArcMap 10.4.1, 10.4, 10.3.1, 10.3, 10.2.2, 10.2.1, 10.2, 10.1, 10