ArcPy Field Calculator >> PLEASE HELP!

1091
6
Jump to solution
06-19-2018 08:53 AM
by Anonymous User
Not applicable

I need some assistance creating a ArcPy script to field calculate and display the value of a field based on the input of another field. I would like to show the value of attribute NS_Address if the Prefix attribute is N or S and I would like to show the value of attribute EW_Address if the Prefix attribute is E or W.

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Make the address field active.

put the function in the code block.

the expression in the expression box

and calculate away

def my_func(val, fld1, fld2)
    """ NS_Address if the Prefix attribute is N or S
        EW_Address if the Prefix attribute is E or W
    """
    if val in ('N', 'S'):
        return fld1
    elif val in ('E', 'W'):
        return fld2
    else:
        return None‍‍‍‍‍‍‍‍‍‍

expression

my_func(!Prefix!, !NS_Address!, !EW_Address! )

View solution in original post

6 Replies
forestknutsen1
MVP Regular Contributor

Something like this? Untested code...

http://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm
https://www.tutorialspoint.com/python/string_split.htm

Python IF...ELIF...ELSE Statements 

import arcpy
feature_class_path =r"xxxx"

with arcpy.da.UpdateCursor(feature_class_path, ["NS_Address", "Address"]) as cursor:
    for row in cursor:
        row_parts = row.split(" ")
        prefix = ""
        if row_parts[0] == "N" or row_parts[0] == "S":
            prefix = "NS_Address"
        else:
            prefix = "EW_Address"
        row[1] = prefix + " " + row_parts[1] + " " + row_parts[2]
        cursor.updateRow(row)
0 Kudos
DanPatterson_Retired
MVP Emeritus

Make the address field active.

put the function in the code block.

the expression in the expression box

and calculate away

def my_func(val, fld1, fld2)
    """ NS_Address if the Prefix attribute is N or S
        EW_Address if the Prefix attribute is E or W
    """
    if val in ('N', 'S'):
        return fld1
    elif val in ('E', 'W'):
        return fld2
    else:
        return None‍‍‍‍‍‍‍‍‍‍

expression

my_func(!Prefix!, !NS_Address!, !EW_Address! )

by Anonymous User
Not applicable

Dan,

Thanks for the reply. I received the error below when attempting to run that calculation.

0 Kudos
by Anonymous User
Not applicable

I just had to add a colon after the first line. The expression ran perfectly! Thank you!!!

0 Kudos
DanPatterson_Retired
MVP Emeritus

Anthony, no problem

You should close the question so others can find the answer

0 Kudos
DanPatterson_Retired
MVP Emeritus

Assumed Answered....

I suppose

0 Kudos