I'm trying to reclass values in field calculator using Python.
I want any value>= '5' to remain the same (return same value). Anything else to be = '0'.
Yes, DIST_EDGE are numbers.
I'm getting an invalid syntax error in Line 1 (see below)
Here's what I came up with (new script following comments):
enter image description here
Error Message:
enter image description here
-
1Is the data type for DIST_EDGE string or numeric? String values are accessed with quotes, numbers are not.phloem– phloem2015年01月07日 17:06:04 +00:00Commented Jan 7, 2015 at 17:06
-
Once you call the function Reclass(!POINTS_Adelard.DIST_EDGE!), the passed value is stored in a variable, no '!'. I've updated my answer below.phloem– phloem2015年01月07日 18:37:24 +00:00Commented Jan 7, 2015 at 18:37
1 Answer 1
Assuming the data type for DIST_EDGE is numeric, the function should be (note, no quotes):
def Reclass(dist):
if dist >= 5:
return dist
else:
return 0
However, if DIST_EDGE is a string data type, you must cast it to a number before making your comparison:
def Reclass(dist):
if int(dist) >= 5:
return dist
else:
return '0'
-
3These functions will work. The actual problem with the OP's code is that his function is using
!POINTS_Adelard.DIST_EDGE!
as a parameter name, which is of course invalid Python syntax. Replacing those references in the function (but not the final function call) withdist
(as in @phloem's examples) will resolve this.nmpeterson– nmpeterson2015年01月07日 18:57:02 +00:00Commented Jan 7, 2015 at 18:57
Explore related questions
See similar questions with these tags.