Select to view content in your preferred language

Field calculation issues

593
2
Jump to solution
07-07-2021 12:00 PM
KatHoenke
Emerging Contributor

I'm trying to calculate a field from another field using arcpy. If the field contains the word Diversion, I want it to calculate the other field with the value 3. If it conctains 'Dam' I want it to calculate 9, otherwise keep it null.

 

When I try to execute this in the python window, itlooks like it's working, but my field is still null. The new field is a long integer. Please Help!

Category = "StructureCategory"
expression5 = "Reclass(!StructureT!)"
codeblock = """def Reclass (newdams_schema):
    if 'Diversion' in newdams_schema:
        return 3
    elif 'Dam' in newdams_schema:
        return 9
    else:
        return None"""

# Execute CalculateField 
arcpy.CalculateField_management(newdams_schema, Category, expression5, "PYTHON_9.3")

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

are you doing this in arcmap or arcgis pro? (because PYTHON3 is used in Pro)

I think you missed codeblock.

Also, you don't need to return None and you can format your codeblock as

Category = "StructureCategory"
expression5 = "Reclass(!StructureT!)"
codeblock = """
def Reclass (newdams_schema):
    if 'Diversion' in newdams_schema:
        return 3
    elif 'Dam' in newdams_schema:
        return 9
    else:
        return
"""
arcpy.management.CalculateField(newdams_schema,
                                field=Category,
                                expression=expression5,
                                expression_type="PYTHON",
                                code_block=codeblock)

 


... sort of retired...

View solution in original post

2 Replies
DanPatterson
MVP Esteemed Contributor

are you doing this in arcmap or arcgis pro? (because PYTHON3 is used in Pro)

I think you missed codeblock.

Also, you don't need to return None and you can format your codeblock as

Category = "StructureCategory"
expression5 = "Reclass(!StructureT!)"
codeblock = """
def Reclass (newdams_schema):
    if 'Diversion' in newdams_schema:
        return 3
    elif 'Dam' in newdams_schema:
        return 9
    else:
        return
"""
arcpy.management.CalculateField(newdams_schema,
                                field=Category,
                                expression=expression5,
                                expression_type="PYTHON",
                                code_block=codeblock)

 


... sort of retired...
KatHoenke
Emerging Contributor

Thank you!

0 Kudos