Populating string field based on numeric value from numeric field in ArcGIS Field Calculator?
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"
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.
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.