1

I'd like to have a tool or quick python script to classify an existing number field to a new text field. For example I have a roads layer that has a type identifier as a coded number and I would like to create a new field that described the number code as text. So say the existing field 'FCODE" had code "210" , and I want to write "Highway" in a new field "Type" and do that for multiple number-text pairs. I could select by attribute and classify that way but it would be nice to do it all in one step.

1

If you want this as a python script, then you could do something with an update cursor...

inputData = "" #Needs to be the full path
codeField = ""
updateField = ""

with arcpy.da.UpdateCursor(inputData, [codeField, updateField]) as updateRows:
    for row in updateRows:
        if row[0] == "TestValue1":
            row[1] = "NewValue"
        elif #... etc
        else:
            #If you want, put something here to catch any values outside of your test
        updateRows.updateRow(row)

You could also do it in the field calculator using a code block, make sure to check off the python Parser and select "Show Codeblock". In the Code Block box put:

def test(testField):
    if testField == "TestValue1":
        return "NewValue"
    elif testField == "TestValue2"
        return "NewValue2"
    elif #... etc

Then where it says "FIELD NAME =" (the update field) put:

test(!testFieldName!)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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