4

I am new to Python and I'm trying to write a simple if then statement in Field Calculator in ArcGIS 10.2.2 that will take the values from a numeric field and add them to a string field. I can't figure out which python function to use. When i try this code, it works but all values in my text field become "N/A."

This is what I put in the Pre-logic Script Code:

def change(Value):
    if Value == 1:
      return "Developed"
    elif Value == 2:
      return "Agriculture"
    elif Value == 3:
      return "Forested"
    elif Value == 4:
      return "Water"
    elif Value == 5:
      return "Wetlands"
    else:
      return "N/A"

enter image description here

9
  • Sometimes it's best just to cast everything as a string, in case the object or field type is the issue. Try if Value == "1" etc. and changing the expression to change(str(!Type!)). BTW, in your question you can add 4 spaces in front of your code in to get the nice formatting.
    – mr.adam
    Jul 14, 2015 at 15:27
  • What data type is the Type field, and what field are you running the field calc on?
    – ianbroad
    Jul 14, 2015 at 15:35
  • The type field is a text field and I'm running it on Type. It is still not working when I treated everything as a string like you said. Thanks!
    – GMod
    Jul 14, 2015 at 15:39
  • Could you please describe how you are implementing this with the field calculator? For example, are you trying to add values to the "type" field by referencing values from the "Value" field?
    – Aaron
    Jul 14, 2015 at 15:51
  • 1
    Ah, then you should be using !Value! because the Value field values are the ones you want to have passed through your code-block. You can probably remove the string casting idea that I had. So go back to change(!Value!) and Value == 1. Understand that in the code block, you are not actually referencing any fields or the values in them, you are just defining how the values that expression are handled.
    – mr.adam
    Jul 14, 2015 at 16:02

2 Answers 2

3

Your problem was just that you were passing the wrong field to the function. Think about it this way:

Field calculator produces a new value for each row a given field (the field shown just above the expression where it says "Type =") based on the expression. In the expression, you are able to reference other fields by using !Field_name!. By using the code-block, you can create functions. You can reference these functions in the expression, which gives you the opportunity to pass values from other fields to the functions you have written in the code block.

1

Your code logic seems fine, you just have to change the bottom script to:

Type = change(Value)

You are getting a "N/A" as a value on all of your columns because you are running it on the [Type] column of data where none of the values are 1-5 as you've expected, instead of running that function on the [Value] column, where those numbers(i'm assuming) are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.