1

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

2

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)
  • Thanks! Do you know of any good resources for learning python with GIS? – Ben D. Oct 4 at 2:53
  • No, I picked it up as I went along. Try gis.stackexchange.com/search?q=learning+python – user2856 Oct 4 at 3:43
  • @BenD On Udemy there is ArcPy for Python Developers using ArcGIS Pro which is an eLearning course I wrote and recorded. – PolyGeo Oct 4 at 11:40

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.