Select to view content in your preferred language

Trying to reclassify a field in attribute table with Python in Field Calculator

7849
7
Jump to solution
07-10-2015 11:04 AM
DeidreA
Emerging Contributor

Hello again!

I'm still relatively new to Python and it's my first time implementing it in the Field Calculator. What I am trying to accomplish is the reclassification of one field "CAUSE" into a new blank field named "CAUSE_RGR". I was given a script that I would have to adapt to this current project.

Basically I need to reclassify the numbers in the "CAUSE" column to be new numbers in the "CAUSE_RGR" as so:

1 --> 7

3 --> 2

4 --> 1

I was given a script that I would have to adapt to this current project, so I watched a tutorial on using python in the field calculator and came up with this script in the field calculator:

Pre-Logic Script Code:

def Reclass (CAUSE):

        if CAUSE == 1:

             return 7

        elif CAUSE == 3:

             return 2

        elif CAUSE == 4:

             return 1

        else:

             return 0

CAUSE_RGR =

def Reclass(!CAUSE!)

However I keep getting errors. Is it a syntax issue? I can't see why something as this isn't working.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Strange... seems to work just fine:

... with this result:

View solution in original post

0 Kudos
7 Replies
XanderBakker
Esri Esteemed Contributor

You should probably only use

CAUSE_RGR =

def Reclass(!CAUSE!)

... so without the "def"

DeidreA
Emerging Contributor

Thanks for the advice, but there still seems to be an issue. I removed it, but I'm still getting no result in my attribute table. I also checked the Geoprocessing Results window and it's telling me my script ran successfully, but it's not reflected in the table at all.

0 Kudos
XanderBakker
Esri Esteemed Contributor

... and you don't have a small selection activated of your table? (Field Calculator works on selections). I am going to create a test dataset with some value to see if I can reproduce this. What version of ArcGIS for Desktop are you using?

DeidreA
Emerging Contributor

Actually, I just figured it out! The problem is that although I was using numbers from the original "CAUSE" field, I forgot I had set the entire field to be a string, so it should look like

def Reclass (CAUSE):

  if CAUSE == "1":

    return 7

  elif CAUSE == "3":

    return 2

  elif CAUSE == "4":

    return 1

  else:

    return 0

With quotation marks. Silly me! Thank you for your help!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Glad you resolved it. The datatype of the field has a large influence over the interpretation of the values (strings in this case).

0 Kudos
XanderBakker
Esri Esteemed Contributor

Strange... seems to work just fine:

... with this result:

0 Kudos
DanPatterson_Retired
MVP Emeritus

Xander is correct, you never call a function with 'def' attached.  It is also a good idea to check your syntax and issues by running a script outside of arcmap in an python IDE.  Substitute possible values in your test def using the fieldname as the incrementer remembering to add ! before and after in the field calculator.  Now here is your example...but you will see that it will soon become ridiculous in length if there are many cases in your CAUSE's and you would be advised to examine other data structures to facilitate logic and access to reclassification (for example dictionaries etc etc).  BUT that is not the issue here... Just make sure that you don't get Lost in your CAUSE syntax

def fld_reclass (case):
    if case == 1:
        return 7
    elif case == 3:
        return 2
    elif case == 4:
        return 1
    else:
        return 0
possible = [-1,0,1,2,3,4,5]
for CAUSE in possible:            # if it helps, use your fieldname as the incrementer
    result = fld_reclass(CAUSE)  # now don't forget the !_before&after_!
    print("case {}  result {}".format(CAUSE,result))
#