[GIS] Using if/elif/else statements in ArcGIS Field Calculator

arcgis-10.0arcgis-desktopfield-calculatorpython

I have no experience with Field Calculator in ArcGIS 10.

I have a very simple calculation I would like to perform that I could do easily via pure SQL but just can't seem to get my head around it in the Field Calculator.

I have a table of 5000 weeds of which there are around 10 types.
I want to update one field in the table (called CLASS) to be either A, B, or C depending on another field: WEED_NAME

So, if WEED_NAME = 'Hyptis' then Class = 'A'
else if WEED_NAME = 'Parkinsonia' then Class = 'B' etc

Best Answer

It's quite similar to Using conditional statements in Field Calculator, but this code should do the trick:

Pre-logic block:

def ifBlock(weedName):
 if weedName== 'Hyptis':
  return "A"
 elif weedName== 'Parkinsonia':
  return "B"
 elif weedName== 'xxxx':
  return "C"

Then in the actual code:

ifBlock(!WEED_NAME!)

Make sure you select Python as the script engine

Related Question