English

How To: Calculate fields based on the input of two other fields in ArcMap

Summary

The instructions provided describe how to calculate fields based on the input of two other fields in ArcMap. In this example, a Python code is used to calculate new values for a field based on the residential and commercial unit fields. A new field is created in the attribute table to indicate whether the input fields match and identify the values that do not match.

In the figure below, two of the rows of the attribute table have residential and commercial unit values that do not match, and the other two rows have matching values.
An image of two fields in the attribute table.

Procedure

To calculate fields based on the input of two other fields:

  1. In ArcMap, right-click the selected layer in Table Of Contents, and select Open Attribute Table.

    An image of selecting Open Attribute Table.
     
  2. In the attribute table window, create a new field. To do this, refer to ArcGIS Desktop Help: Adding fields.
  3. In the Editor toolbar, click Editor > Start Editing.
  4. Right-click the newly created field, and select Field Calculator.

    An image of opening the field calculator.
     
  5. In the Field Calculator, select the Python parser, and check the Show Codeblock check box.
  6. In the Pre-Logic Script Code box, copy and paste the following code:
    def calc(field1 , field2 ):
        if field1 == field2 :
            return 'Yes'
        else:
            return 'No' 
  7. Copy and paste the following code in the second box:
    calc( !Field1! , !Field2! )
  8. Replace !Field1! and !Field2! by double-clicking the field in the Fields list.

    An image showing the Field Calculator.
     
  9. Click OK and the values for the new field are calculated, as shown in the image below.

    An image showing the calculated new field.
     
  10. In the Editor toolbar, select Stop Editing and Save Edits to keep the changes.

Related Information