If-Then function for writing a value in column B based on a value in column A

1049
10
Jump to solution
05-27-2020 09:46 AM
RobertHill
New Contributor II

Hello All,

I'm looking for a starting point on how to code the following in ArcPy: I need an UpdateCursor to go through every row in my feature class and write a value into a cell based on the value in another cell.  In other words, if I have two fields (RR Type and RR Width), for each row where RR Type = "Two Way," I want to write the value "12" in RR Width.

I know an UpdateCurser is needed and am thinking that I should use an If - Then function, but it's a bit over my head.  I'm still really new to Python.  

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

If you want to use cursors, use the Data Access cursors (UpdateCursor—Help | Documentation ) and not the original cursors.

import arcpy

fc = # path to feature class or shape file
field1 = "LTN"
field2 = "ZI016_WD1"

with arcpy.da.UpdateCursor(fc, [field1, field2]) as cur:
    for row in cur:
        if row[0] == 1:
            row[1] = 4
        else:
            row[1] = 3 * row[0]
        cur.updateRow(row)

View solution in original post

10 Replies
JoshuaBixby
MVP Esteemed Contributor

If you only have one condition to update, you don't need an update cursor.  For your specific situation, using the Field Calculator and put the following in the expression box using the Python parser:

12 if !RRTYPE! == "Two Way" else !RRWidth!
RobertHill
New Contributor II

Thank you, so I have 3 conditions--3 RR Types that need corresponding values. So I may be looking at an "if, then, else" function. And I would like to avoid the Field Calculator because I'm trying to automate this as much as possible.  It's a recurring task that will need to be performed on a lot of different tables over coming months.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You can still use the Field Calculator, and it might be easier than using a cursor if you are new to Python.  As Joe pointed out, you can use a basic if/elif/else statement, but you need to share your conditions so people can provide specific feedback.

JoeBorgione
MVP Emeritus
if <condtion 1>:
   do something
elif <condition 2>:
   do something else
else:
   do the third something‍‍‍‍‍‍
That should just about do it....
RobertHill
New Contributor II

Thank you both for your feedback. Above is where I am at this point in my code, and here's what I'm trying to achieve: Where "LTN" = 1, I need "ZI016_WD1" to = 4.  In all other cases I need "ZI016_WD1" to = 3x the "LTN" value.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you want to use cursors, use the Data Access cursors (UpdateCursor—Help | Documentation ) and not the original cursors.

import arcpy

fc = # path to feature class or shape file
field1 = "LTN"
field2 = "ZI016_WD1"

with arcpy.da.UpdateCursor(fc, [field1, field2]) as cur:
    for row in cur:
        if row[0] == 1:
            row[1] = 4
        else:
            row[1] = 3 * row[0]
        cur.updateRow(row)
JoeBorgione
MVP Emeritus

First... Repeat after me:  I will always use the arcpy.daUdpateCursor and never use the old and outdated arcpy.UpdateCursor.

With that out of the way, here is how I would approach it:

fc = r'C:\some\path\to\my\data

fields = ['LTN', 'ZI016_WD1']

with arcp.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if row[0] == 1:
            row[1] = 4
        else:
             row[1] = row[0] * 3
        cursor.updateRow‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I would also suggest not using shapefiles and if you must, don't keep them in C:\program files.  In fact, don't put any  data there.

BTW: your error in line 10 is because you used = instead of ==.....

hahahaha: looks like Joshua Bixby‌ hit enter just before I did....

That should just about do it....
JoshuaBixby
MVP Esteemed Contributor

I know the feeling, it has happened to me many times....

0 Kudos
RobertHill
New Contributor II

Good feedback, thanks.  What would you use instead of a shapefile?  And where do you recommend storing your data instead?

0 Kudos