I have gotten stuck on seemingly simple ArcGIS field calculator python expression.

I obtained a great answer to a another similar question a few years ago: Using Range in Python expression of ArcGIS Field Calculator?. Now I need to populate a new field based on 6 ranges of values (bedrock depth).

My attempt at writing this expression is below:

enter image description here

I just need to populate the empty field "bedrockdp" using the ranges in the toc called brockdepmin.

screen shot

Pre-Logic Script Code:

def !GRIDCODE! (value):
    if value >0 and value < 50:
        return "0 - 50 cm from the surface"
        return "n/a"

bedrockdep =

GRIDCODE (!bedrockdep!)

1 Answer 1


I think the problem lies with your statement:

def !gridcode! (value):

is incorrect, see http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000

def something(value):

It's hard to read what you've got there but I think this is it: Python expression

Full expression:

def TextValue(FromCode):
    if FromCode > 0 and FromCode < 50:
        return "0 - 50 cm from the surface"
    elif FromCode < 150:
        return "101 - 150 cm"
    elif FromCode < 200:
        return "151 - 200 cm"
    elif FromCode < 250:
        return "201 - 250 cm"
    elif FromCode < 1000:
        return "251 - 1000 cm"
        return "N/A"

This will populate the text that is in the legend using the GRIDCODE as the numeric source.

  • i.sstatic.net/JEmEH.png is link to the screenshot
    – sirgeo
    Commented Jun 2, 2014 at 3:56
  • Fair enough, the problem is that you're using !gridcode! as the function name. The field bedrockdepmin is the one that gets the exclamation marks. It has to do with the scope of the variable.. only the lower box allows substitution, the pre-logic code deals only with variables and the return value is what is populated into the selected field. I can't see the field that you are using to calculate from brockdepmin in the screenshot, is it there? or is it the GRIDCODE? Commented Jun 2, 2014 at 4:15
  • GRIDCODE is the only value from the attribute table being used. The toc has the 6 classes that need to be added to the bedrockdep field based on the GRIDCODE field. I will give your code a shot in the morning and post the results here. THANKS
    – sirgeo
    Commented Jun 2, 2014 at 5:05
  • 1
    good answer, but I recommend using the parentheses in the condition: if (FromCode > 0) and (FromCode < 50):
    – radouxju
    Commented Jun 2, 2014 at 8:54
  • 2
    >= is the operator for greater or equal. Commented Jun 2, 2014 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.