ModelBuilder Calculate Field

1386
5
Jump to solution
02-29-2012 12:25 PM
WadeGivens
New Contributor II
I'm using ModelBuilder to create a new field ([cat]) that will contain category values based on range criteria of another field ([yield]).  I would like to use the Python parser.  HEre is what I have so far in the Pre-Logic Script Code:

def Reclass(yield) :
  if (yield <=140) :
    return "Low"
  elif (yield > 140 and yield <= 180) :
    return "Average"
  elif (yield > 180) :


In the expression block I have cat = Reclass(!cat!)

I get a red "X" in the calculate Field dialog stating there is a parse error on line 1.  Any ideas as to what I'm doing wrong?

Thanks,
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TerrySilveus
Occasional Contributor III
you can't use yield it is a reserved word

also what you posted doesn't see complete
try this
def Reclass(x):
  if (x <=140):
    return "Low"
  elif (x > 140 and x <= 180):
    return "Average"
  elif (x > 180):
    return "High"

View solution in original post

0 Kudos
5 Replies
TerrySilveus
Occasional Contributor III
you can't use yield it is a reserved word

also what you posted doesn't see complete
try this
def Reclass(x):
  if (x <=140):
    return "Low"
  elif (x > 140 and x <= 180):
    return "Average"
  elif (x > 180):
    return "High"
0 Kudos
BruceNielsen
Occasional Contributor III
Your expression block should be:
cat = Reclass(!yield!)
Otherwise you'd be trying to read an integer from a string field.
0 Kudos
TerrySilveus
Occasional Contributor III
Your expression block should be:
cat = Reclass(!yield!)
Otherwise you'd be trying to read an integer from a string field.


"Yield" within the function still needs to be changed as well, but you are right you can't pass a string when the function is looking for a number.  Therefore cat = Reclass(!cat!) should be changed to cat = Reclass(!nameOfFieldwithNumbericValue!) unless cat is the field with the numeric value; in that case it should be nameOfFieldWithStringValue = Reclass(!Cat!)
0 Kudos
TedKowal
Occasional Contributor III

or perhaps  

cat = Reclass(int(!cat!))   # --- If you need to use the cat value and overwrite the string number with text
                            # --- Then you would also have to account for possible non numbers text in the Reclass def?‍‍‍‍‍‍‍‍
0 Kudos
curtvprice
MVP Esteemed Contributor

The expression for the Calculate Field tool should simply be (if "cat" is your field name). CAT is also then entered in the Field argument to Calculate Field.

Ted is correct that you'd want to account for non-covered fields. Seems to me it would be safer to create a new field and calculate to that in case something goes wrong.

Reclass(!CAT!)
0 Kudos