How To: Calculate fields based on the input of two other fields in ArcMap
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.
To calculate fields based on the input of two other fields:
- In ArcMap, right-click the selected layer in Table Of Contents, and select Open Attribute Table.
- In the attribute table window, create a new field. To do this, refer to ArcGIS Desktop Help: Adding fields.
- In the Editor toolbar, click Editor > Start Editing.
- Right-click the newly created field, and select Field Calculator.
- In the Field Calculator, select the Python parser, and check the Show Codeblock check box.
- 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'
- Copy and paste the following code in the second box:
calc( !Field1! , !Field2! )
- Replace !Field1! and !Field2! by double-clicking the field in the Fields list.
- Click OK and the values for the new field are calculated, as shown in the image below.
- In the Editor toolbar, select Stop Editing and Save Edits to keep the changes.
- ArcGIS Desktop Help: Calculate Field examples
- ArcGIS Desktop Help: Making simple field calculations
- ArcGIS Desktop Help: Adding fields