<?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 Re: ArcPy Field Calculation within Loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262243#M20155</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Joshua,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks! That fixed the problem.&amp;nbsp; I guess it was something simple after all. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 01 Apr 2016 20:19:42 GMT</pubDate>
    <dc:creator>PatrickMcKinney1</dc:creator>
    <dc:date>2016-04-01T20:19:42Z</dc:date>
    <item>
      <title>ArcPy Field Calculation within Loop</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262238#M20150</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am writing an ArcPy script that will run from a tool with user inputs.&amp;nbsp; The user will select an input layer and output name/directory.&amp;nbsp; I have my current code below.&amp;nbsp; The tool will then do the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Loop through each record in the input layer and do the following within each loop cycle:&lt;OL&gt;&lt;LI&gt;clip a census blocks layer by the input feature&lt;/LI&gt;&lt;LI&gt;Create a field in the output clip layer to hold an area ratio value&lt;/LI&gt;&lt;LI&gt;Create a field in the output clip layer to hold an estimated population value&lt;/LI&gt;&lt;LI&gt;Perform a field calculation between two fields in the output clip layer to produce the area ratio (new area divided into original area)&lt;/LI&gt;&lt;LI&gt;Perform a field calculation bewteen two fields in the output clip layer to produce the estimated population (area ratio value calculated in #4 mulitplied by the population field)&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let's say the input layer contains three records (1500-ft, 2000-ft, and 2500-ft).&amp;nbsp; There would be three output layers that are clipped census block layers based upon the records from the input, each with estimated census populations based upon the distances from the input feature.&amp;nbsp; The layers would have names like Output_ft_1500, Output_ft_2000, and Output_ft_2500.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My code can do everything except complete the field calculations.&amp;nbsp; I'm not sure if I'm missing something simple with the syntax for a SQL expression using fields or what.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please let me know if you have any questions. I tried to explain things as best as I could.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the error I get:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; File "F:\Scripts\ArcGIS Geoprocessing\SARA Tool\EstimateCensusPopulation.py", line 73, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldExpression = newArea / orgArea&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;TypeError: unsupported operand type(s) for /: 'Field' and 'Field'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;Failed to execute (EstimateCensusPopulation).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;----------------&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Import arcpy
import arcpy

#Set workspace

# Sara Facility
sara = arcpy.GetParameterAsText(0)

# Clip Feature - U.S. Census Blocks
censusBlocks = r'\\CCPASR07\ncgs$\Scripts\ArcGIS Geoprocessing\SARA Tool\SARA_Tool_DEV.gdb\CensusBlocks_2010'

# Output
output = arcpy.GetParameterAsText(1)


# Search cursor
cursor = arcpy.SearchCursor(sara)

for row in cursor:
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Clip features
&amp;nbsp;&amp;nbsp;&amp;nbsp; feat = row.Shape
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Append buffer distance and units to name
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Buffer distance
&amp;nbsp;&amp;nbsp;&amp;nbsp; buffDist = str(int(row.BUFFDIST))
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Buffer units to name
&amp;nbsp;&amp;nbsp;&amp;nbsp; buffUnits = row.UNITS
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Appended output name variable
&amp;nbsp;&amp;nbsp;&amp;nbsp; buffAppend = '_' + buffUnits + '_' + buffDist


&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execute clip tool on each row
&amp;nbsp;&amp;nbsp;&amp;nbsp; newInput = arcpy.Clip_analysis(censusBlocks, feat, output + buffAppend)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add message that Clip is completed
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Feature Clip operation completed')

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add field to hold clip area to original area ratio
&amp;nbsp;&amp;nbsp;&amp;nbsp; areaRatioFieldName = 'AREARATIO'
&amp;nbsp;&amp;nbsp;&amp;nbsp; areaRatioFieldType = 'DOUBLE'
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execut tool
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(newInput, areaRatioFieldName, areaRatioFieldType)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add message that Area Ratio Field has been added
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Area Ratio field added')

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add field to hold estimated population
&amp;nbsp;&amp;nbsp;&amp;nbsp; estPopFieldName = 'ESTPOP'
&amp;nbsp;&amp;nbsp;&amp;nbsp; estPopFieldType = 'LONG'
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execut tool
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(newInput, estPopFieldName, estPopFieldType)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add message that Estimated Population Field has been added
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Estimated Population field added')

&amp;nbsp;&amp;nbsp;&amp;nbsp; ### Code does not work after this point ###
&amp;nbsp;&amp;nbsp;&amp;nbsp; areaInField = arcpy.ListFields(newInput, 'AREARATIO')[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; newArea = arcpy.ListFields(newInput, 'Shape_Area')[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; orgArea = arcpy.ListFields(newInput, 'ORAREA')[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldExpression = newArea / orgArea
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(newInput, areaInField, fieldExpression, 'PYTHON_9.3')

del cursor&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:51:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262238#M20150</guid>
      <dc:creator>PatrickMcKinney1</dc:creator>
      <dc:date>2021-12-11T12:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy Field Calculation within Loop</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262239#M20151</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Patrick,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Consider blocking your code properly in the thread to make it easier to read:&lt;/P&gt;&lt;P&gt;Use Advanced Editor &amp;gt; double arrows &amp;gt; syntax &amp;gt; python&lt;/P&gt;&lt;P&gt;&lt;IMG alt="PythonSyntaxGeoNet.PNG" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/193517_PythonSyntaxGeoNet.PNG" style="width: auto; height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, consider using arcpy.da.SearchCursor instead of arcpy.SearchCursor as the "da" method is newer and faster.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Where is the code throwing an error? Do you get an error message?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Mar 2016 14:40:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262239#M20151</guid>
      <dc:creator>AdrianWelsh</dc:creator>
      <dc:date>2016-03-31T14:40:08Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy Field Calculation within Loop</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262240#M20152</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "F:\Scripts\ArcGIS Geoprocessing\SARA Tool\EstimateCensusPopulation.py", line 73, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldExpression = newArea / orgArea&lt;/P&gt;&lt;P&gt;TypeError: unsupported operand type(s) for /: 'Field' and 'Field'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Failed to execute (EstimateCensusPopulation).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Mar 2016 14:49:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262240#M20152</guid>
      <dc:creator>PatrickMcKinney1</dc:creator>
      <dc:date>2016-03-31T14:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy Field Calculation within Loop</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262241#M20153</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hmmm, at this point, I would put in some print statements for debugging to see what is actually being returned by "newArea" and "orgArea" to make sure they are valid and to make sure it is what you expected. Is it certain that those two are numbers and that they are divisible with each other?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Mar 2016 15:04:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262241#M20153</guid>
      <dc:creator>AdrianWelsh</dc:creator>
      <dc:date>2016-03-31T15:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy Field Calculation within Loop</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262242#M20154</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your expression needs to be a string representation of your expression.&amp;nbsp; In Line 63 above, you are not trying to create an expression but divide two Field data types, which isn't supported.&amp;nbsp; Also, be careful when building your expression that you use field names and not field objects.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Try replacing Lines 60-64 with:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;fieldExpression = '!Shape_Area! / !ORAREA!'
arcpy.CalculateField_management(newInput, 'AREARATIO', fieldExpression, 'PYTHON_9.3') &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:51:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262242#M20154</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T12:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy Field Calculation within Loop</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262243#M20155</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Joshua,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks! That fixed the problem.&amp;nbsp; I guess it was something simple after all. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Apr 2016 20:19:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-field-calculation-within-loop/m-p/262243#M20155</guid>
      <dc:creator>PatrickMcKinney1</dc:creator>
      <dc:date>2016-04-01T20:19:42Z</dc:date>
    </item>
  </channel>
</rss>

