[GIS] ArcGIS Pro Field Calculator Python IF statement

arcgis-profield-calculatorpython-parser

I'm attempting to populate a newly created field with values dependent on an existing field.

The new field has text data type. The existing field is double data type. This is the python code I've been trying. It runs, but the values remain

def Reclass(Crime):
    if (Hierarchy == 1):
        return "Murder-Manslaughter"
    elif (Hierarchy == 2):
        return "Forcible Rape"
    elif (Hierarchy == 3):
        return "Robbery"
    elif (Hierarchy == 4):
        return "Aggravated Assault"

enter image description here

Best Answer

You need to pass the Hierarchy field to the function, not the Crime field.

crime = 
Reclass(!Hierarchy!)

def Reclass(Hierarchy):
    if (Hierarchy == 1):
        return "Murder-Manslaughter"
    elif (Hierarchy == 2):
        return "Forcible Rape"
    elif (Hierarchy == 3):
        return "Robbery"
    elif (Hierarchy == 4):
        return "Aggravated Assault"

Or even neater, use a dict lookup:

def Reclass(Hierarchy):
    reclass = {
        1: "Murder-Manslaughter",
        2: "Forcible Rape",
        3: "Robbery",
        4: "Aggravated Assault",
    }
    return reclass.get(Hierarchy)
Related Question