[GIS] IF/ELIF statement returning NULL for values that are not calculated with field calculator in Python

arcpyfield-calculatorpython-parser

I am using field calculator to change several values of field. I am receiving NULL values for those which are not in my function. What is the cause of this and a workaround?

In the code below; I would like to change only the values in my function. If a value is not in the function; the code automatically sets the new value as NULL.

import arcpy

codeblock = """def replacestring(DAMAGING_PARTY):
        output = None
        if DAMAGING_PARTY == "Arizona Pipeline contact is Chad":
            output = "Arizona Pipeline Company"
        elif DAMAGING_PARTY == "James A Shirley Construction":
             output = "James A. Shirley Construction, Inc."
        elif DAMAGING_PARTY == "NPL Construction (Kevin Sena)":
            output = "NPL Construction"
        elif DAMAGING_PARTY == "NPL Construction DENNIS PARKHOUSE":
            output = "NPL Construction"
        elif DAMAGING_PARTY == "Rami / Benito Sustaita":
             output = "Rami Alabassi"
        elif DAMAGING_PARTY == "Shea Homes (Contractor Working for)":
             output = "Shea Homes"
        elif DAMAGING_PARTY == "Shea Homes/Rudy Andrade":
             output = "Shea Homes"
        elif DAMAGING_PARTY == "W A RASIC CONSTRUCTION/CODY ROLLER":
             output = "W A RASIC CONSTRUCTION"
        elif DAMAGING_PARTY == "WARASICK CONSTR COMPANY/GIANCARLO PAMDOSI":
             output = "W A RASIC CONSTRUCTION"
        elif DAMAGING_PARTY == "Young Construction; contact Rick Musmecci":
             output = "Young Construction"
        return output"""

ShortCodeExpression = "replacestring(!DAMAGING_PARTY!)"

table = r'table'

arcpy.CalculateField_management(table, "DAMAGING_PARTY", ShortCodeExpression, "PYTHON_9.3", codeblock)

Best Answer

You are setting output = None at the beginning of your script, so anything that doesn't specifically set output is returned as a NULL at the end when you return the value. Instead of setting output at the start, just return each value in your if/elif chain, and then an else to return the current value.

codeblock = """def replacestring(DAMAGING_PARTY):
    if DAMAGING_PARTY == "Arizona Pipeline contact is Chad":
        return "Arizona Pipeline Company"
    elif DAMAGING_PARTY == "James A Shirley Construction":
         return "James A. Shirley Construction, Inc."
    elif DAMAGING_PARTY == "NPL Construction (Kevin Sena)":
        return "NPL Construction"
    elif DAMAGING_PARTY == "NPL Construction DENNIS PARKHOUSE":
        return "NPL Construction"
    elif DAMAGING_PARTY == "Rami / Benito Sustaita":
         return "Rami Alabassi"
    elif DAMAGING_PARTY == "Shea Homes (Contractor Working for)":
         return "Shea Homes"
    elif DAMAGING_PARTY == "Shea Homes/Rudy Andrade":
         return "Shea Homes"
    elif DAMAGING_PARTY == "W A RASIC CONSTRUCTION/CODY ROLLER":
         return "W A RASIC CONSTRUCTION"
    elif DAMAGING_PARTY == "WARASICK CONSTR COMPANY/GIANCARLO PAMDOSI":
         return "W A RASIC CONSTRUCTION"
    elif DAMAGING_PARTY == "Young Construction; contact Rick Musmecci":
         return "Young Construction"
    else:
         return DAMAGING_PARTY 
    """
Related Question