<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic da.UpDateCursor to conditionally calculate a field based on test of another field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452890#M35490</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I was testing out arcpy.da.UpdateCursor to calculate a field based on another fields value.&amp;nbsp; My script runs without error, but doesn't seem to make any changes in the shapefile.&amp;nbsp; I tried adding the line overwriteOutput = "True" but that didn't work.&amp;nbsp; The print line I added for debugging shows the correct values for ReddStatus or row[1], but they don't get saved in the output.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt; # Import arcpy module import arcpy from arcpy import env env.workspace = "C:\\avdata\\PythonTest\\" arcpy.env.overwriteOutput = "True"&amp;nbsp; # Local variables: inshape = "zilltogra101013.shp" #input shapefile field1 = "PassNum" #field with test values field2 = "ReddStatus" #field to be calculated fields = ['PassNum', 'ReddStatus']&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor(inshape, fields) as cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row[0] == 1: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = "Definite" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif row[0] == 2: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = "Probable" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row[0],row[1], '\n'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 23 May 2014 22:22:05 GMT</pubDate>
    <dc:creator>PaulHuffman</dc:creator>
    <dc:date>2014-05-23T22:22:05Z</dc:date>
    <item>
      <title>da.UpDateCursor to conditionally calculate a field based on test of another field</title>
      <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452890#M35490</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I was testing out arcpy.da.UpdateCursor to calculate a field based on another fields value.&amp;nbsp; My script runs without error, but doesn't seem to make any changes in the shapefile.&amp;nbsp; I tried adding the line overwriteOutput = "True" but that didn't work.&amp;nbsp; The print line I added for debugging shows the correct values for ReddStatus or row[1], but they don't get saved in the output.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt; # Import arcpy module import arcpy from arcpy import env env.workspace = "C:\\avdata\\PythonTest\\" arcpy.env.overwriteOutput = "True"&amp;nbsp; # Local variables: inshape = "zilltogra101013.shp" #input shapefile field1 = "PassNum" #field with test values field2 = "ReddStatus" #field to be calculated fields = ['PassNum', 'ReddStatus']&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor(inshape, fields) as cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row[0] == 1: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = "Definite" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif row[0] == 2: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = "Probable" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row[0],row[1], '\n'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 May 2014 22:22:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452890#M35490</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2014-05-23T22:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: da.UpDateCursor to conditionally calculate a field based on test of another field</title>
      <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452891#M35491</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Apparently I just need to add a line cursor.updateRow(row) at the end of the loop to write the changes. (As shown in the documentation, if I would ever just read farther along. )&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But another problem showed up.&amp;nbsp; I was trying to show a colleague how to do this, but he only has ArcGIS 10.0, so he doesn't have da.UpdateCursor.&amp;nbsp; I need to come up with a way using just arcpy.UpdateCursor.&amp;nbsp; I've tried a couple different loops but come up with either the input file doesn't exist or the field field1 doesn't exist.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Import arcpy module
import arcpy
from arcpy import env
env.workspace = "C:\\avdata\\PythonTest\\testgdb.gbd\\"

# Local variables:
fc = "zilltogra" #input Feature class
field1 = "PassNum" #field with test values
field2 = "ReddStatus" #field to be calculated

items = arcpy.ListFields(fc)
print items
##cursor = arcpy.UpdateCursor(inshape)
##row = cursor.next()
##while row:
##&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.field1 == 1:
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.field2 = "Definite"
##&amp;nbsp;&amp;nbsp;&amp;nbsp; elif row.field1 == 2:
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.field2 = "Probable"
##&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updaterow(row)
##&amp;nbsp;&amp;nbsp;&amp;nbsp; row = cursor.next(1)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
rows = arcpy.UpdateCursor(fc)&amp;nbsp;&amp;nbsp;&amp;nbsp; 
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.field1 == 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.field2 = "Definite"
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif row.field1 == 2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.field2 = "Probable"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Import arcpy module
import arcpy
from arcpy import env
env.workspace = "C:\\avdata\\PythonTest\\"

# Local variables:
inshape = "zilltogra101013.shp" #input shapefile
field1 = "PassNum" #field with test values
field2 = "ReddStatus" #field to be calculated
fields = ['PassNum', 'ReddStatus'] 

##cursor = arcpy.da.UpdateCursor(inshape)
##row = cursor.next()
##while row:
##&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.field1 == 1:
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.field2 = "Definite"
##&amp;nbsp;&amp;nbsp;&amp;nbsp; elif row.field1 == 2:
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.field2 = "Probable"
##&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updaterow(row)
##&amp;nbsp;&amp;nbsp;&amp;nbsp; row = cursor.next(1)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
rows = arcpy.UpdateCursor(inshape)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.field1 == 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.field2 = "Definite"
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif row.field1 == 2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.field2 = "Probable"
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:12:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452891#M35491</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2021-12-11T20:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: da.UpDateCursor to conditionally calculate a field based on test of another field</title>
      <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452892#M35492</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You still have to actually update the row. Sounds like the same issue as your first post, based on the code you posted.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;edit: I think the main problem may be where you assign the field names to the variables field1 and field2. Arcpy doesn't like this with the . notation. It looks for an actual field named field1, which doesn't exist. Technically, it's looking for a property of the row class with the value field1, when the value is PassNum. Try row.PassNum and row.ReddStatus.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;edit redux: note that this would be incorrect when using da, which returns an index based tuple and is referenced by index number.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 May 2014 22:58:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452892#M35492</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2014-05-23T22:58:05Z</dc:date>
    </item>
    <item>
      <title>Re: da.UpDateCursor to conditionally calculate a field based on test of another field</title>
      <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452893#M35493</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The "cursor.field" syntax doesn't work with variables, just field names. Here's how you get the values if the field is a variable. And, as was mentioned - don't forget the updateRow again -- you need that to write the data to the table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;rows = arcpy.UpdateCursor(inshape)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(field1) == 1: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(field2, "Definite") &amp;nbsp;&amp;nbsp;&amp;nbsp; elif row.getValue(field1) == 2: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(field2, "Probable") &amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 24 May 2014 01:26:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452893#M35493</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-05-24T01:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: da.UpDateCursor to conditionally calculate a field based on test of another field</title>
      <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452894#M35494</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks guys.&amp;nbsp; All I needed to add was&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;del row
del rows&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;to release the locks on the attribute table.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:12:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452894#M35494</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2021-12-11T20:12:10Z</dc:date>
    </item>
    <item>
      <title>Re: da.UpDateCursor to conditionally calculate a field based on test of another field</title>
      <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452895#M35495</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What's wrong with this approach,&amp;nbsp; using Field Calculator with a simple python function, no cursors?&amp;nbsp; Works well on my small test set.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]34118[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 May 2014 21:07:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452895#M35495</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2014-05-27T21:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: da.UpDateCursor to conditionally calculate a field based on test of another field</title>
      <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452896#M35496</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, sure, spoil all the fun and use the field calculator!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Yeah, that looks fine...but look at it this way:&amp;nbsp; now you have learned another option (probably a faster option) to use da cursor, and you don't need to open ArcMap, open the table, get to the field calculator, etc etc in order to execute a script.&amp;nbsp; In a script you can also, when needed, insert messaging and such on more complex operations to monitor or even trap where things go wrong on certain records.&amp;nbsp; When the field calculator fails, everything comes to a screeching halt.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 May 2014 00:22:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452896#M35496</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2014-05-28T00:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: da.UpDateCursor to conditionally calculate a field based on test of another field</title>
      <link>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452897#M35497</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The guy I was helping with this told me that he had tried using the field calculator, but was overwhelmed by its complexity, so I steered away from the field calculator too.&amp;nbsp;&amp;nbsp; Now he has two ways to do this. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using da.cursor without opening ArcMap will be nice with large data sets,&amp;nbsp; but I couldn't notice the difference with my little test data set.&amp;nbsp;&amp;nbsp; The longest part seemed to taken just loading arcpy.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My workflows often involve having ArcMap open so I can view and edit feature classes as I run through them. But that doesn't mean I can't drag some da.cursor code into the ArcMap python window for this kind of calculation.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 May 2014 20:23:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/da-updatecursor-to-conditionally-calculate-a-field/m-p/452897#M35497</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2014-05-28T20:23:25Z</dc:date>
    </item>
  </channel>
</rss>

