jessica.alvarez

if then statement in field calculator, string to integer

Discussion created by jessica.alvarez on Mar 4, 2014
Latest reply on Mar 4, 2014 by jessica.alvarez
I am having a heck of a time trying to write an if elif script in the field calculator. I've read numerous threads but can't figure out how to do it right. I have a field called route_ty_1 that is a string field, with possible attributes of "Bus," "Rail," "LightRail," etc. For a given attribute, I want a code number to be returned for the site_code field, which is short integer. Here is the code I have been tinkering with:

Pre-logic script code:
def Reclass(site_code):
  if route_ty_1 == 'Bus':
    return 338
  elif route_ty_1 == 'LIGHT RAIL':
    return 281
  elif route_ty_1 == 'METRO':
    return 335
  elif route_ty_1 == 'Rail':
    return 282
  elif route_ty_1 == 'Commuter Bus':
    return 336


site_code =
Reclass(!site_code!)


If there is a better way to do this than if statements I am very open to suggestions! Thanks!

Outcomes