Field Calculator and Python If statement

2158
7
04-06-2020 06:05 PM
ChristopherCowin
New Contributor II

Hi,

Trying to replicate the code from this guide: Calculate Field Python examples—Data Management toolbox | Documentation 

Which is:

Expression:

   Reclass(!WELL_YIELD!)

Code Block:

   def Reclass(WellYield):

   if (WellYield >= 0 and WellYield <= 10):

      return 1

   elif (WellYield > 10 and WellYield <= 20):

      return 2

   elif (WellYield > 20 and WellYield <= 30):

      return 3 elif (WellYield > 30):

   return 4

My Code:

Expression:

   Reclass(!Id!)

Code Block

   def Reclass(Level):
      if (Id >= 0 and Id <= 10):
         return 1
      elif (Id > 10 and Id <= 20):
         return 2   
      elif (Id > 20 and Id <= 30):
         return 3
      elif (Id > 30 and Id <= 40):
         return 4
      elif (Id > 40 and Id <= 50):
         return 5
      elif (Id > 50 and Id <= 60):
         return 6
      elif (Id > 60 and Id <= 70):
         return 7
      elif (Id > 70 and Id <= 80):
         return 8
      elif (Id > 80 and Id <= 90):
         return 9
      elif (Id > 90 and Id <= 100):
         return 10

This is the error

 ERROR 000539: Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "<string>", line 2, in Reclass
NameError: name 'Id' is not defined
Failed to execute (CalculateField).

I don't know what Name Id is not defined means as i defined it just like the example.

7 Replies
DanPatterson_Retired
MVP Emeritus

def Reclass(Level):

becomes

def Reclass(Id):

You need to replace Level by Id without the ! marks.  or... replace all the Id 's in your code block by Level.

If isn't a good idea to use the field name inside the def, but you did

ChrisCowin
New Contributor III

Hi Dan,

This is something that I had originally tried, the Level there was just the most recent thing I had tried and when I ran out of things to try I came here. When I try either change you suggest I get a "parameter missing error" in red text at the bottom of the window.

Why isn't it a good idea to use a field name and what would I use alternatively?

Here is a picture of the window if that helps.

JosphatMutunga
Esri Contributor

Hello Chris,

You are headed in the right direction here. COuld you send a screenshot of the error details? Since the code validated successfully, I suspect that the 'Id' field data type is not a numerical type (short/long integer or double) since the code is trying to write such values there. 

Regards,

Josphat. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The structure of your if/elif lends itself quite naturally to Python bisect — Array bisection algorithm — Python 3.8.2 documentation.

Expression:

Reclass(!Id!)‍‍

Code Block:

import bisect

def Reclass(Id):
    return bisect.bisect_left(range(10,100,10),Id) + 1‍‍‍‍‍‍‍‍
ChrisCowin
New Contributor III

Unfortunately I get the exact same error as I did before...

I tried changing ID to Level just to try and it gave me a new error but i think it got farther into the code.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The issue in the first screenshot is that you are not passing the field correctly.  If the field in the GDB is called, Field1, then you pass it in the expression box as, !Field1! .The Id variable that is defined by the function is only used within the function.  The variable name for the parameter in the function and the field in the GDB don't have to match.

My original code has a typo, I should have capitalized I in id on the usage in bisect function.  I have updated the code.  In your case, if the parameter name is "Level", then you need to use "Level" in the code and not "level".  Python is case sensitive.

0 Kudos
DanPatterson_Retired
MVP Emeritus

also check that the field containing the dat is indeed Id and doesn't have any trailing space.

And if you have any nulls in the field, you will have to edit your code OR, just query the field for all values no equal to None eg <null>

0 Kudos