Select to view content in your preferred language

Reclass python script not working

4963
12
Jump to solution
08-14-2012 10:47 AM
JenniferDick
Deactivated User
Hi,

I'm trying to reclass a vector field that has values from 1 to 7. I would like to reclass these values to 1's & 2's. For example:

if the value is 1 reclass it to 2
if the value is 2 reclass it to 2
if the value is 3 reclass it to 1

and so on.

Here is my script, but it is not working...any thoughts?

def Reclass( !Value! 😞
  if ( !Value! = 1):
    return 2
  if ( !Value! = 2):
    return 2
  if ( !Value! = 3):
    return1
  if ( !Value! = 4):
    return 1
  if ( !Value! = 5):
    return 1
  if ( !Value! = 6):
    return 1
  if ( !Value! = 7):
    return 1


reclass( !Value! )
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BruceBacia
Frequent Contributor
this worked for me.  You might want to double check that you are selecting "Python" at the top.  I forgot several times while
testing 🙂

def Reclass(n):     valueDict = {1 : 2, 2 : 2, 3 : 1, 4 : 1, 5 : 1, 6 : 1, 7 : 1}      return valueDict   Weight =  Reclass(!Value!) 

View solution in original post

0 Kudos
12 Replies
SolomonPulapkura
Frequent Contributor
try double equals sign to compare
if ( !Value! == 1):


also your function call should be Reclass(!Value!)

Is Value the name of the field?
0 Kudos
BruceBacia
Frequent Contributor
Using a dictionary, you can avoid all of the if/thens

def Reclass( !Value! ):
    valueDict = {1 : 2, 2 : 2,
                     3 : 1, 4: 1,
                     5 : 1, 6 : 1,
                     7: 1} 
    !VALUE! = valueDict[!VALUE!]
0 Kudos
JenniferDick
Deactivated User
try double equals sign to compare
if ( !Value! == 1):


also your function call should be Reclass(!Value!)

Is Value the name of the field?


Yes, Value is the name of the field
0 Kudos
JenniferDick
Deactivated User
Thank you for your input...

I've tried both options and still no success.

Here is the error message I receive:

Failed to execute. Parameters are not valid
ERROR 000989: Python syntax error: Parsing error (type 'exceptions.SyntaxError'>: invalid syntax (line 1)
Failed to execute (CalculateField)
0 Kudos
SolomonPulapkura
Frequent Contributor
Try
def Reclass(n):
if ( n == 1):
return 2
....


Reclass( !Value! )
0 Kudos
JenniferDick
Deactivated User
[ATTACH=CONFIG]16950[/ATTACH]Still receive a generic error...

I've attached a screen shot of the field calculator. Note that the Weight field is the column I would like the changes to be placed and the Value field is what I want to reclassify.

I appreciate your time!
0 Kudos
BruceBacia
Frequent Contributor
Are these fields both integer?
0 Kudos
JenniferDick
Deactivated User
The Value field is a Float and the Weight field is integer
0 Kudos
BruceBacia
Frequent Contributor
this worked for me.  You might want to double check that you are selecting "Python" at the top.  I forgot several times while
testing 🙂

def Reclass(n):     valueDict = {1 : 2, 2 : 2, 3 : 1, 4 : 1, 5 : 1, 6 : 1, 7 : 1}      return valueDict   Weight =  Reclass(!Value!) 
0 Kudos