<?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: Finding sum of values in field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327624#M25465</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Along different lines, you can do this in the field calculator.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;expression:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;func()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;codeblock:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def func():
&amp;nbsp;&amp;nbsp;&amp;nbsp; sum = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = "H:/GIS_Data/TEMP.gdb/points" # the path to your feature class

&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc)

&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum = row.THEFIELDTOSUM + sum # enter the fieldname in this line
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; return sum
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 15:31:09 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2021-12-11T15:31:09Z</dc:date>
    <item>
      <title>Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327617#M25458</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a cities feature class with a field for the Population in the attribute table. I want to find the sum of all the values in the Population field which will give me the total population of all the cities. I think I might have to use the arcpy.CalculateField_management function but I am confused on the syntax I should be using, or maybe there is a different way I should be writing the script. Any help would be much appreciated!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Oct 2011 19:46:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327617#M25458</guid>
      <dc:creator>StephenFricke</dc:creator>
      <dc:date>2011-10-11T19:46:48Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327618#M25459</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You could use the Summarize tool without a case field and sum the population.&amp;nbsp; You could add a temporary field to both tables with a dummy value like 1 and join the result to your original layer on that field.&amp;nbsp; Then you can calculate over the resulting summed value to all of the records or get percentages.&amp;nbsp; Or else add the dummy field to your original layer first and use that as the Case field for the summary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A cursor in python could also do the sum and write the result, but not the field calculator.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Oct 2011 21:56:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327618#M25459</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2011-10-11T21:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327619#M25460</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Could you tell me what the syntax would be for finding the sum of a field with a cursor?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Oct 2011 22:36:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327619#M25460</guid>
      <dc:creator>StephenFricke</dc:creator>
      <dc:date>2011-10-11T22:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327620#M25461</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Peter,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example on how to do this.&amp;nbsp; The below code will append all the rows from the population field to a list using a search cursor, then it will sum the list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

fc = "Cities"

list = []

rows = arcpy.SearchCursor(fc)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pop = row.getValue("POPULATION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(pop)

print sum(list) &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327620#M25461</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T15:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327621#M25462</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey Jake, thanks!&amp;nbsp; That was a lot of help.&amp;nbsp; I am still getting an error when I run my script saying &amp;lt;type 'exceptions.TypeError'&amp;gt;: coercing to Unicode: need string or buffer, int found.&amp;nbsp; Could you please take a look at the script and let me know what the error might be?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inWorkspace = "C:/TEMP/Tooldata/Florida.gdb"
arcpy.env.workspace = inWorkspace
inPoly = "C:/TEMP/Tooldata/Florida.gdb/Counties"
inPoint = "C:/TEMP/Tooldata/Florida.gdb/Cities"
destField = "URBPOP"
msg = "Workspace is " + arcpy.env.workspace
arcpy.AddMessage(msg)
fcList = arcpy.ListFeatureClasses()
msg = "\nFeature classes in " + inWorkspace + ":"
arcpy.AddMessage(msg)
for anFC in fcList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + anFC
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
fldList = arcpy.ListFields(inPoly)
msg = "\nFields in " + inPoly + ":"
arcpy.AddMessage(msg)
for aFld in fldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + aFld.name
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
fieldPresent = False
for aFld in fldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if aFld.name == destField:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldPresent = True
if not fieldPresent:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(inPoly,destField,"DOUBLE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = "\nNew Field " + destField + " created in " + inPoly
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
arcpy.MakeFeatureLayer_management(inPoly,"allPolys")
arcpy.MakeFeatureLayer_management(inPoint,"allPoints")
polyRows = None
polyRow = None
polyRows = arcpy.UpdateCursor(inPoly)
msg = '\nUpdate cursor created for ' + inPoly + '. Records are:'
arcpy.AddMessage(msg)
for polyRow in polyRows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; firstPart = '"NAME_1" ='
&amp;nbsp;&amp;nbsp;&amp;nbsp; lastPart = "'" + '' + "'"
&amp;nbsp;&amp;nbsp;&amp;nbsp; where_clause = firstPart + lastPart
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(inPoly,"oneCounty",where_clause)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("allPoints", "WITHIN", "oneCounty")
&amp;nbsp;&amp;nbsp;&amp;nbsp; list= []
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = "allPoints"
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pop = row.getValue("POP_98")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(pop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + sum(list)
&amp;nbsp;&amp;nbsp;&amp;nbsp; UrbanPop= sum(list)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
&amp;nbsp;&amp;nbsp;&amp;nbsp; polyRow.URBPOP = int(UrbanPop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("oneCounty")

if polyRow:
&amp;nbsp;&amp;nbsp;&amp;nbsp; del polyRow
if polyRows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; del polyRows
arcpy.Delete_management("allPolys")
arcpy.Delete_management("allPoints")

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The first part of the script is not important, the last part where I am trying to sum the population of cities for each county is the part that I am struggling with.&amp;nbsp; Let me know if you see what I'm doing wrong.&amp;nbsp; Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327621#M25462</guid>
      <dc:creator>StephenFricke</dc:creator>
      <dc:date>2021-12-11T15:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327622#M25463</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This line is probably the problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + sum(list)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;You can't concatenate strings and ints. The blow line should fix it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " +&lt;STRONG&gt; str(sum(list))&lt;/STRONG&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I didn't see anywhere else where that error is made, but you'd need to change it if you are putting together strings and numeric types anywhere else as well.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Oct 2011 17:10:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327622#M25463</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2011-10-12T17:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327623#M25464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I believe you will also need to add another line for the update to occur.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for polyRow in polyRows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; firstPart = '"NAME_1" ='
&amp;nbsp;&amp;nbsp;&amp;nbsp; lastPart = "'" + '' + "'"
&amp;nbsp;&amp;nbsp;&amp;nbsp; where_clause = firstPart + lastPart
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(inPoly,"oneCounty",where_clause)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("allPoints", "WITHIN", "oneCounty")
&amp;nbsp;&amp;nbsp;&amp;nbsp; list= []
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = "allPoints"
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pop = row.getValue("POP_98")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(pop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + sum(list)
&amp;nbsp;&amp;nbsp;&amp;nbsp; UrbanPop= sum(list)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
&amp;nbsp;&amp;nbsp;&amp;nbsp; polyRow.URBPOP = int(UrbanPop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;polyRows.updateRow(polyRow)&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("oneCounty")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327623#M25464</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T15:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327624#M25465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Along different lines, you can do this in the field calculator.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;expression:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;func()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;codeblock:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def func():
&amp;nbsp;&amp;nbsp;&amp;nbsp; sum = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = "H:/GIS_Data/TEMP.gdb/points" # the path to your feature class

&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc)

&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum = row.THEFIELDTOSUM + sum # enter the fieldname in this line
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; return sum
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327624#M25465</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T15:31:09Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327625#M25466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey thanks for the help everyone.&amp;nbsp; I am getting my script to run but when it runs the urban population for every county is being printed as 0.&amp;nbsp; Can anyone tell me why this would be with the way the script currently is?&amp;nbsp; I also tried the way dkwiens suggested and I am also getting zero for the Urban population in each county.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inWorkspace = "C:/TEMP/Tooldata/Florida.gdb"
arcpy.env.workspace = inWorkspace
inPoly = "C:/TEMP/Tooldata/Florida.gdb/Counties"
inPoint = "C:/TEMP/Tooldata/Florida.gdb/Cities"
destField = "URBPOP"

fieldPresent = False
for aFld in fldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if aFld.name == destField:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldPresent = True
if not fieldPresent:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(inPoly,destField,"DOUBLE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = "\nNew Field " + destField + " created in " + inPoly
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
arcpy.MakeFeatureLayer_management(inPoly,"allPolys")
arcpy.MakeFeatureLayer_management(inPoint,"allPoints")
polyRows = None
polyRow = None
polyRows = arcpy.UpdateCursor(inPoly)
msg = '\nUpdate cursor created for ' + inPoly + '. Records are:'
arcpy.AddMessage(msg)
for polyRow in polyRows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; firstPart = '"NAME_1" ='
&amp;nbsp;&amp;nbsp;&amp;nbsp; lastPart = "'" + '' + "'"
&amp;nbsp;&amp;nbsp;&amp;nbsp; where_clause = firstPart + lastPart
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(inPoly,"oneCounty",where_clause)
&amp;nbsp;&amp;nbsp;&amp;nbsp; targetCities = arcpy.SelectLayerByLocation_management("allPoints", "WITHIN", "oneCounty")
&amp;nbsp;&amp;nbsp;&amp;nbsp; list= []
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(targetCities)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pop = row.getValue("POP_98")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(pop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + polyRow.NAME_1 + " - " + "Urban Population: " + str(sum(list))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
&amp;nbsp;&amp;nbsp;&amp;nbsp; UrbanPop= sum(list)
&amp;nbsp;&amp;nbsp;&amp;nbsp; polyRow.URBPOP = int(UrbanPop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; polyRows.updateRow(polyRow)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("oneCounty")

if polyRow:
&amp;nbsp;&amp;nbsp;&amp;nbsp; del polyRow
if polyRows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; del polyRows
arcpy.Delete_management("allPolys")
arcpy.Delete_management("allPoints")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327625#M25466</guid>
      <dc:creator>StephenFricke</dc:creator>
      <dc:date>2021-12-11T15:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327626#M25467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;From the looks of your code I believe you are trying to select all cities within a county, sum the population of the cities and update a field in the counties feature class with this value.&amp;nbsp; You can easily do this by creating a spatial join.&amp;nbsp; You can right-click on the counties feature class in ArcMap's table of contents &amp;gt; Joins and Relates &amp;gt; Joins.&amp;nbsp; Specify to join attributes spatially and choose to sum the fields.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This will produce a new output feature class with the summed population (and summed outputs of all other numeric fields) of all cities within each county.&amp;nbsp; If you do not want a new feature class, and the other summed numeric fields, you can use python.&amp;nbsp; Here is the code that I was able to get to work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
env.overwriteOutput = True

cities = "Cities"
counties = "Counties"

arcpy.MakeFeatureLayer_management(cities, "cities_feat")

list = []

# Get max OBJECTID for loop
rows = arcpy.SearchCursor(counties)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; OID = row.getValue("OBJECTID")
&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(OID)

maxOID = list[-1]

del row, rows

x = 1
while x &amp;lt;= maxOID:
&amp;nbsp;&amp;nbsp;&amp;nbsp; list2 = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor("cities_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pop = row.getValue("POPULATION")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list2.append(pop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; sumlist = sum(list2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor("counties_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row2.URBPOP = sumlist
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2.updateRow(row2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1

del row, rows, row2, rows2&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327626#M25467</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T15:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327627#M25468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.workspace = r"C:\temp\python\test.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.overwriteOutput = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;cities = "Cities"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;counties = "Counties"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;city_layer = "cities_feat"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;county_layer = "counties_feat"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.MakeFeatureLayer_management(cities, city_layer)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in arcpy.SearchCursor(counties):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; county_OID = row.OBJECTID&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(counties, county_layer,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&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;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'OBJECTID = ' + str(county_OID))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(city_layer, 'WITHIN', county_layer)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; list2 = [r.POPULATION for r in arcpy.SearchCursor(city_layer)]&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sumlist = sum(list2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; county_rows = arcpy.UpdateCursor("counties_feat")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for layer_row in county_rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer_row.setValue('URBPOP', sumlist)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; county_rows.updateRow(row2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del county_rows, row, layer_row&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Oct 2011 13:30:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327627#M25468</guid>
      <dc:creator>MarcNakleh</dc:creator>
      <dc:date>2011-10-14T13:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327628#M25469</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey Jake,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Right on! Spatial Join would've been what I'd recommend, though the Python code to do the same work isn't too long and exposes people to a lot of the ideas behind cursors and layers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As a point of information, you could probably speed up this code by using a couple of nifty tricks:&lt;/SPAN&gt;&lt;BR /&gt;&lt;OL&gt;&lt;BR /&gt;&lt;LI&gt;In general, list comprehensions are much faster than iterated item appends. There's always the risk of things getting messy, but I think your code is a great example of a case where it would be easy to slide it in.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;You seem to take the max OID and then just iterate for all numbers between it. You could make that code faster by only iterating through the items of your first list (&lt;SPAN style="font-style:italic;"&gt;for valid_OID in list:&lt;/SPAN&gt;) or just iterating through the rows of the full feature class directly.&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
arcpy.env.workspace = r"C:\temp\python\test.gdb"
arcpy.env.overwriteOutput = True

cities = "Cities"
counties = "Counties"
city_layer = "cities_feat"
county_layer = "counties_feat"

arcpy.MakeFeatureLayer_management(cities, city_layer)

for row in arcpy.SearchCursor(counties):
&amp;nbsp;&amp;nbsp;&amp;nbsp; county_OID = row.OBJECTID
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(counties, county_layer, 'OBJECTID = ' + str(county_OID))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(city_layer, 'WITHIN', county_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; list2 = [r.POPULATION for r in arcpy.SearchCursor(city_layer)]&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; sumlist = sum(list2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; county_rows = arcpy.UpdateCursor(county_layer )
&amp;nbsp;&amp;nbsp;&amp;nbsp; for layer_row in county_rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer_row.URBPOP = sumlist
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; county_rows.updateRow(row2)

del county_rows, row, layer_row&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I hope this is of use!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327628#M25469</guid>
      <dc:creator>MarcNakleh</dc:creator>
      <dc:date>2021-12-11T15:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327629#M25470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey thank you all very much for your help!&amp;nbsp; I have written the script like JSkinn suggested and it worked great.&amp;nbsp; The only problem is at the end when I say to delete row and row2 there is an error because they aren't defined.&amp;nbsp; The exact error message is:&amp;nbsp; &amp;lt;type 'exceptions.NameError'&amp;gt;: name 'row2' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute ().&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyone know how I can fix this?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
cities = "Cities"
counties = "Counties"
destField= "URBPOP"
env.workspace = "C:\TEMP\Tooldata\Florida.gdb"
env.overwriteOutput = True
fldList = arcpy.ListFields(counties)
msg = "\nFields in " + counties + ":"
arcpy.AddMessage(msg)
for aFld in fldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + aFld.name
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
fieldPresent = False
for aFld in fldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if aFld.name == destField:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldPresent = True
if not fieldPresent:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(counties,destField,"DOUBLE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = "\nNew Field " + destField + " created in " + counties
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)

arcpy.MakeFeatureLayer_management(cities, "cities_feat")

list = []

rows = arcpy.SearchCursor("Counties")

for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; OID = row.getValue("NAME_1")
&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(OID)

maxOID = list[-1]

del row, rows

x = 1
while x &amp;lt;= maxOID:
&amp;nbsp;&amp;nbsp;&amp;nbsp; list2 = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor("cities_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pop = row.getValue("POP_98")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list2.append(pop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; sumlist = sum(list2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor("counties_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row2.URBPOP = sumlist
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2.updateRow(row2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + row2.NAME_1 + " - " + "Urban Population: " + str(sumlist)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, rows, row2, rows2
arcpy.Delete_management("counties_feat")
arcpy.Delete_management("cities_feat")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327629#M25470</guid>
      <dc:creator>StephenFricke</dc:creator>
      <dc:date>2021-12-11T15:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327630#M25471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try placing the syntax to delete the cursors outside of the while loop:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;while x &amp;lt;= maxOID:
&amp;nbsp;&amp;nbsp;&amp;nbsp; list2 = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor("cities_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pop = row.getValue("POP_98")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list2.append(pop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; sumlist = sum(list2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor("counties_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row2.URBPOP = sumlist
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2.updateRow(row2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + row2.NAME_1 + " - " + "Urban Population: " + str(sumlist)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1
&lt;STRONG&gt;del row, rows, row2, rows2&lt;/STRONG&gt;
arcpy.Delete_management("counties_feat")
arcpy.Delete_management("cities_feat")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327630#M25471</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T15:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327631#M25472</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the suggestion JSkinn, I have tried as you suggested also and when I do this the loop doesn't stop, and continually gives the last county an Urban Pop value or zero over and over....&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Try placing the syntax to delete the cursors outside of the while loop:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;while x &amp;lt;= maxOID:
&amp;nbsp;&amp;nbsp;&amp;nbsp; list2 = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor("cities_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pop = row.getValue("POP_98")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list2.append(pop)
&amp;nbsp;&amp;nbsp;&amp;nbsp; sumlist = sum(list2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor("counties_feat")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row2.URBPOP = sumlist
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2.updateRow(row2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + row2.NAME_1 + " - " + "Urban Population: " + str(sumlist)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1
&lt;STRONG&gt;del row, rows, row2, rows2&lt;/STRONG&gt;
arcpy.Delete_management("counties_feat")
arcpy.Delete_management("cities_feat")&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327631#M25472</guid>
      <dc:creator>StephenFricke</dc:creator>
      <dc:date>2021-12-11T15:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327632#M25473</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Does anyone have any suggestions on how I should correct the script?&amp;nbsp; Thank you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 16 Oct 2011 03:59:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327632#M25473</guid>
      <dc:creator>StephenFricke</dc:creator>
      <dc:date>2011-10-16T03:59:51Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327633#M25474</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I could not reproduce the behavior you are experiencing.&amp;nbsp; Placing this syntax outside the loop created no errors, and the script executed successfully.&amp;nbsp; I'm not sure why the script is running in a continuous loop for you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I placed the 'del row, rows....' inside the while loop I would receive "NameError: name 'row' is not defined" error message.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If the script is executing successfully with this error message, you can run an except to pass this error within the while loop:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
try:
&amp;nbsp; del row, rows, row2, rows2
except NameError:
&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Though, this may cause the script to skip over some counties and not update the 'URBPOP' field correctly.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327633#M25474</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T15:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327634#M25475</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I ran into a situation where the results of an UpdateCursor was empty.&amp;nbsp; After a "for row in rows:" loop, the "del row" generated and error and prevents the script from finishing.&amp;nbsp; I tried using the None object without sucess, i.e. "if row == None: del row".&amp;nbsp; I'm pretty basic when it comes to python, so this try statement really helped me out.&amp;nbsp; Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jan 2016 01:09:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327634#M25475</guid>
      <dc:creator>AlanKilgore</dc:creator>
      <dc:date>2016-01-29T01:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Finding sum of values in field</title>
      <link>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327635#M25476</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to automatically calculate a field based on values from previous rows?&lt;/P&gt;&lt;P&gt;I need to automatically calculate a field based on values from previous rows such that once the total of all the previous rows is greater than 450 and less than 500, the next row starts &lt;/P&gt;&lt;P&gt;counting from 0 or the balance of what made the previous row greater than 500.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The script below just calculates the sum of the cities point that falls within the county, I want something that would make the value of a row (i.e. row2) the addition of the value of row1 + value of row2 and so on till when it gets to 500, where it starts from 1 again. Kindly help me please.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;cities = "Cities"&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; counties = "Counties"&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; destField= "URBPOP"&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; env.workspace = "C:\Users\KUNLE\Documents\ArcGIS\Default.gdb"&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; env.overwriteOutput = True&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldList = arcpy.ListFields(counties)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = "\nFields in " + counties + ":"&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for aFld in fldList:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = " - " + aFld.name&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldPresent = False&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for aFld in fldList:&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 aFld.name == destField:&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;&amp;nbsp; fieldPresent = True&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not fieldPresent:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(counties,destField,"DOUBLE")&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = "\nNew Field " + destField + " created in " + counties&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(cities, "cities_feat")&amp;nbsp; 
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list = []&amp;nbsp; 
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor("Counties")&amp;nbsp; 
&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OID = row.getValue("POP")&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(OID)&amp;nbsp; 
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; maxOID = list[-1]&amp;nbsp; 
&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, rows&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = 1&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while x &amp;lt;= maxOID:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list2 = []&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(counties, "counties_feat", "OBJECTID = " + str(x))&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("cities_feat", "WITHIN", "counties_feat")&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor("cities_feat")&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;&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; pop = row.getValue("POPULATION")&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;&amp;nbsp; list2.append(pop)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sumlist = sum(list2)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor("counties_feat")&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:&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;&amp;nbsp; row2.URBPOP = sumlist&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;&amp;nbsp; rows2.updateRow(row2)&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;&amp;nbsp;&amp;nbsp; #msg = " - " + row2.POP + " - " + "Urban Population: " + str(sumlist)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.AddMessage(msg)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, rows, row2, rows2&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("counties_feat")&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("cities_feat")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:31:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-sum-of-values-in-field/m-p/327635#M25476</guid>
      <dc:creator>OLANIYANOLAKUNLE</dc:creator>
      <dc:date>2021-12-11T15:31:30Z</dc:date>
    </item>
  </channel>
</rss>

