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

Does anyone have tips on how to code this using ArcGIS 10.2 for Desktop?


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
  • Thanks for that. When I try running the function however i get the Error 'Python Syntac Error: Parsing error. SyntaxError: invalid syntax (Line 5)' – John Jul 7 '15 at 0:45
  • Not to worry, found the error. A simple missing colon. Thanks a lot. – John Jul 7 '15 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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