[GIS] ArcMap Calculate Field..if “text” in !Field!

arcgis-desktoparcmapfield-calculator

Is it possible to use Field Calculator in ArcGIS to add text to a new field based on an if in statement?

Example: In an attribute table I have a some fields called !X!, !Y!, !Z! where "A (xx%)", "B (xx%)", "C (xx%)", "D (xx%)" are the data.

The data can be found in any field but does not have to.

I would like to use python in some form so that once I have created a New Field, I can calculate if A in !X! copy A in !X! to New Field then continue this through !Y! and !Z!.

I believe outside of Field Calculator this will involve If in statements followed by arcpy.da.insertcursor() but I'm pretty stumped at the moment.

I hope this is clear…thanks for any help and comments!

Best Answer

Your question is a bit unclear. I try to explain to you what this script does:

You have a column A and a column B. Depending on the values in column A we populate the empty fields in Column B.

change(!A!) #write that into Expression of your Field Calculator,  with the "!" you tell ArcMap in Python that this is a column

def change (A): #everything from here goes into your CodeBlock
    if A == 1: #first if statement
        return b
    elif A == 2: #if the first if statement does not apply, then the next \"elif\" statement goes into action
        return c
    elif A == 3:
        return d #this can be done for as many elif statements as you like
    else: #if non of the if and elif statements apply, then we use the else statement to return a value for all the fields in column A where the above quoted condition DO NOT apply
        return 0

Make sure you chose in Expression Python. make also sure you get your indentation right (four spaces in front of the if, elif and else statement and eight spaces in front of the return

If you want to populate the Column B with a String, just add "" to your return values.

if A == 3:
    return "String"

Hope that helps.

Related Question