I need help in writing the python script in the field calculator. I have three fields namely: CODE COMPLEX Map1
Map1 is an empty field that I want to populate. I have a number of conditions to cater with in a single Map1 field. e.g. If CODE is 1.1.7 and COMPLEX is 4 then the output for Map1 should be 1.3, and these conditions continue with different CODE and COMPLEX values. I have been trying to write it like this but it doesn't work:
Parser Python
Code Block
def Reclass( !CODE! , !COMPLEX! ):
if ( !CODE! == 1.1.7 and !COMPLEX! == 4):
return 1.3
elif ( !CODE! == 3.2.0 and !COMPLEX! == 4):
return 1.3
else:
return 0
Map1 =
Reclass( !CODE! , !COMPLEX! )
Can anyone tell me where I am making a mistake? Or any other way of doing it?
-
The fields in the code block are just variables, no exclamation marks. it should be def Reclass(my_code,my_complex) or something like that. The calling of it in the field box is correct.Michael Stimson– Michael Stimson2014年11月11日 03:05:14 +00:00Commented Nov 11, 2014 at 3:05
-
Thanks Michael for a quick reply. I've edited the code block with no exclamation marks now, but I'm still getting an error. I am new to programming and probably making a simple mistake somewhere :( that I don't know of: Now its like this: def Reclass( my_code,my_complex): if (my_code == 1.1.7 and my_complex == 4): return 1.3 elif (my_code == 3.2.0 and my_complex == 4): return 1.3 else: return 0Juwairia– Juwairia2014年11月11日 03:13:52 +00:00Commented Nov 11, 2014 at 3:13
-
1It's always best to edit your question rather than trying to include code in comments - your formatting options there are far superior. This Meta answer may help with framing and interacting on your questions.PolyGeo– PolyGeo ♦2014年11月11日 03:21:55 +00:00Commented Nov 11, 2014 at 3:21
-
PolyGeo's answer is correct, copy that into your code block and you should be fine. If you're still having problems edit/update your question with the current code block either as text or alt+PrtScn and paste the field calculator dialog. Please ensure that you've selected the python language radio button.Michael Stimson– Michael Stimson2014年11月11日 03:42:34 +00:00Commented Nov 11, 2014 at 3:42
1 Answer 1
The problem is in your code block.
Instead of:
def Reclass( !CODE! , !COMPLEX! ):
if ( !CODE! == 1.1.7 and !COMPLEX! == 4):
return 1.3
elif ( !CODE! == 3.2.0 and !COMPLEX! == 4):
return 1.3
else:
return 0
try:
def Reclass( CODE , COMPLEX ):
if ( CODE == "1.1.7" and COMPLEX == 4):
return 1.3
elif ( CODE == "3.2.0" and COMPLEX == 4):
return 1.3
else:
return 0
The exclamation marks are needed in your expression because there they are indicating field names but in the code block CODE
and COMPLEX
are Python variables and could be called a
and b
if you wanted.
The other problem is that your CODE field appears to be of type text so I have added quotes around the tests of its values. I cannot tell if your Map1 and COMPLEX fields are text or numbers so I have not put them there - you may need to!
-
That's it! See example on this page resources.arcgis.com/en/help/main/10.1/index.html#//…Michael Stimson– Michael Stimson2014年11月11日 03:07:22 +00:00Commented Nov 11, 2014 at 3:07
-
Thanks a lot Michael. Removing the exclamation and changing the data type from Number to String, followed by quoting e.g. '1.1.7' helped. Thanks again :)Juwairia– Juwairia2014年11月11日 03:22:38 +00:00Commented Nov 11, 2014 at 3:22
Explore related questions
See similar questions with these tags.