1

I've never used Python scripting before and can't seem to figure out how to use an If statement in the Field Calculator tool.

I have a field called 'MAX_GRIDCODE' which returns the highest force a piece of infrastructure is exposed to. I need to use this number to calculate a simple estimate of damage in the field 'DI'.

The statements I need are:

If "Max_GRIDCODE" >0 and <=0.5 then "DI" = 1
If "Max_GRIDCODE" >0.5 and <=1 then "DI" = 2
If "Max_GRIDCODE" >1 and <=1.5 then "DI" = 3
If "Max_GRIDCODE" >1.5 and <=2 then "DI" = 4
If "Max_GRIDCODE" >2 then "DI" = 5

How do I code this using ArcGIS 10.2 for Desktop?

2 Answers 2

5

open CalculateField tool

for 'Field Name' parameter specify: DI

for 'Expression' parameter specify: x(!Max_GRIDCODE!)

for 'Code Block' parameter specify the code below

def x(v):
    if v > 2:     return 5
    elif v > 1.5: return 4
    elif v > 1:   return 3
    elif v > 0.5:  return 2
    else: return 1
0
0

Esri Support provide an How To: Use IF statements in the Field Calculator:

Summary

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.