Field Calculator Reclass Python

6800
6
08-30-2012 01:07 PM
SamNeale
New Contributor
I'm trying to assign a float value to field N_Value, based on the string value in field LUCode.  My code runs without errors, but isn't making any changes in the attribute table.

def Reclass(N_Value, LUCode):
  if LUCode == "Pasture":
    return 0.050
  elif LUCode == "Greenspace":
    return 0.04
  elif LUCode == "Forest":
    return 0.08
  elif LUCode == "Urban":
    return 0.04
  elif LUCode == "Brush":
    return 0.060
  elif LUCode == "Grass":
    return 0.035
  elif LUCode == "Clean, straight":
    return 0.030
  elif LUCode == "Clean, straight":
    return 0.035
  elif LUCode == "Straight, weeds/stones":
    return 0.040
  else:
    print "Done."


Reclass(!N_Value!, !LUCode!)


Thank you.
Tags (2)
0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
I'd remove your print statement. I'm not sure what that would do in a field calculator. Also are you using the advanced tab and calculating with python option? Try loading this and see if it does it for you.
0 Kudos
SamNeale
New Contributor
I'm running it with python checked, but I'm not sure which advanced tab you're referring to.

The code runs, but it's still not updating the field. Is there a specific way that a float value needs to be written?

def Reclass(N_Value, LUCode):
  if LUCode == "Pasture":
    return 0.050
  elif LUCode == "Greenspace":
    return 0.04
  elif LUCode == "Forest":
    return 0.08
  elif LUCode == "Urban":
    return 0.04
  elif LUCode == "Brush":
    return 0.060
  elif LUCode == "Grass":
    return 0.035
  elif LUCode == "Clean, straight":
    return 0.030
  elif LUCode == "Clean, winding":
    return 0.035
  elif LUCode == "Straight, weeds/stones":
    return 0.040

Reclass(!N_Value!, !LUCode!)


Thanks
0 Kudos
MathewCoyle
Frequent Contributor
In the field calculator window there is a checkbox called show codeblock, that needs to be checked and your code needs to be split between the two boxes. If the example I sent you isn't working though I'm not sure what the issue is.
0 Kudos
curtvprice
MVP Esteemed Contributor
I'm trying to assign a float value to field N_Value, based on the string value in field LUCode.  My code runs without errors, but isn't making any changes in the attribute table.


I would avoid passing N_Value to the function; you're not using it.

You also may want to consider using a dictionary instead - just nicer, faster code.

The try/except will cause the function return null if there is no match to your list of lucode values.

Calculate Value

Expr:

Reclass(!LUCode!)

Code block:

def Reclass(lucode):
  dict = {
  "Pasture"     : 0.050  ,
  "Greenspace"  : 0.04   ,
  "Forest"      : 0.08   ,
  ...
   }
  try:
    return dict[lucode]
  except:
    pass
0 Kudos
SamNeale
New Contributor
Oh, yeah I have that on.

Thanks for trying.
0 Kudos
SamNeale
New Contributor
Thanks, it worked!  I had tried using a dictionary command before, but I didn't do it right I guess.
0 Kudos