Calculating fields using Python

474
1
09-26-2019 02:50 PM
JeanetteChamorro
New Contributor

I'm having the hardest time coming up with a Python expression to calculate fields based on multiple conditions. I need to be able to associate a material code to a feature that meets two specific conditions. For example:

The logic I'm trying to use is:

if CATEGORY_NAME == TAIL AND Calculated_length < 500 equals MATERIAL_CD =  "10835549"

elseif CATEGORY_NAME == TAIL AND Calculated_length > 500 equals MATERIAL_CD =  "10796850"

My python code looks like this

I'm sure there is something wrong with it but I cannot figure out what it is. Any insight would be greatly appreciated! Thank you.

0 Kudos
1 Reply
DanPatterson_Retired
MVP Emeritus

The logic is as follows

def Reclass(a, b):
    """just make it generic"""
    if a == "TAIL" and b < 500:
        return 10835549
    elif a == "TAIL" and b >= 500:
        return 10796850
    else:
        return -1
        

Reclass("TAIL", 100)
10835549

Reclass("TAIL", 600)
10796850

# ---- in the field calculator, you replace the actual numbers with

Reclass(!CATEGORY_NAME!, !CALCULATEDLENGTH!)

See line 19 for field calculator syntax... you pass the field names containing the values to the function