HOW TO
Note: ArcMap is in Mature support and will be retired March 1, 2026. There are no plans for future releases of ArcMap, and it is recommended that you migrate to ArcGIS Pro. See Migrate from ArcMap to ArcGIS Pro for more information.
The Field Calculator uses IF statements to calculate new values in the field of an attribute table. This article describes some common uses of IF statements in the Field Calculator and with Python scripts.
To access the Field Calculator, refer to ArcMap: Making field calculations for more information. The following scripts use the Python parser with the Show Codeblock option checked and the expression provided.
Note: Indentations are not automatically inserted into the Pre-Logic Script Code box. Follow the exact indentations specified in the script sample for the code to function properly.
Use the IF statement to convert the value of a text field based on the condition set. The following script populates the field with 'Yes' or 'No' if the value in the Valid field is '1' or '0' respectively.
def convert(Valid): if (Valid == '1'): return 'Yes' elif (Valid == '0'): return 'No' else: return 'N/A'
In the Expression text box below the Pre-Logic Script Code section, enter the following:
convert(!Valid!)
Use the IF statement to populate a field based on the value of another field. The following script populates a field with 'Y' or 'N' if the value in the ObjectID_2 field is '1' or '0' respectively.
def reclass(OBJECTID_2): if (OBJECTID_2 == 0): return "N" else: return "Y"
In the Expression text box below the Pre-Logic Script Code section, enter the following:
reclass(!OBJECTID_2!)
A field can be populated with different values according to a range of values from another field. The following script classifies a field according to the different ranges of elevation in the Top_Elevat field, with less than 8,000 ft. classified as Class 1, and so forth.
def reclass(TOP_ELEVAT): if (TOP_ELEVAT < 8000): return 1 elif (TOP_ELEVAT > 8000 and TOP_ELEVAT < 9001): return 2 elif (TOP_ELEVAT > 9000 and TOP_ELEVAT < 10001): return 3 elif (TOP_ELEVAT > 10000): return 4
In the Expression text box below the Pre-Logic Script Code section, enter the following:
reclass(!TOP_ELEVAT!)
Use the .isdigit() function to copy values from one text data type field to another only if the value begins with a number, as shown in the example below.
def myCalc(num): if (num[0].isdigit()): return num else: return " "
In the Expression text box below the Pre-Logic Script Code section, enter the following:
myCalc(!<fieldnamehere>!)
An IF statement can be used to copy the values from one of two fields that meets a certain criteria. The following script copies a value from the Acres or Legal field to a new field based on the value in the Legal field.
def calc(legal, acres): if legal == 0: return acres else: return legal
In the Expression text box below the Pre-Logic Script Code section, enter the following:
calc(!Legal!, !Acres!)
An IF statement can be used to change the value of one field that meets the condition of two other fields. The following script changes the value in the ActivityStatus field to 'Closed', if there is a value of less than -10 in the CountDown field and has an 'Approved' value in the ActivityStatus field. If it does not meet both conditions, the ActivityStatus field is left as it is.
def myCalc(status,activity): if (status<-10)and(activity=='Approved'): return 'Closed' else: return activity
In the Expression text box below the Pre-Logic Script Code section, enter the following:
myCalc(!CountDown!, !ActivityStatus!)
Get help from ArcGIS experts
Download the Esri Support App