<?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: Returning SUM of Values from a Field Within a Selection Set in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351709#M27562</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use the sum() function. For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;zonePolys = arcpy.ListFeatureClasses("Zone*")
for fc in zonePolys:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(fc,"SumOfValues","LONG")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("OriginalLayer", "OriginalLayer_Lyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("OriginalLayer_Lyr","INTERSECT",fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; someValue = sum([r[0] for r in arcpy.da.SearchCursor("OriginalLayer_Lyr",["FIELD_NM"])]) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fc,"SumOfValues",someValue) #haha get it...&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 16:31:36 GMT</pubDate>
    <dc:creator>ChrisSnyder</dc:creator>
    <dc:date>2021-12-11T16:31:36Z</dc:date>
    <item>
      <title>Returning SUM of Values from a Field Within a Selection Set</title>
      <link>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351707#M27560</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Basically I want to select line features based on a polygon feature, then return a value that is the sum of all the features in a field that is within the selected set of line features.&amp;nbsp; Then do this for say, a hundred more feature classes.&amp;nbsp; I figured there would be a tool, called GetSum or something similar that would do this but did not see it in the Help.&amp;nbsp; I've explored using Summary Statistics, but that returns a table, not a simple value.&amp;nbsp; Here is what I've got.&amp;nbsp; Ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# Create a list of all the polygon feature classes zonePolys = arcpy.ListFeatureClasses("Zone*")&amp;nbsp; for fc in zonePolys: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(fc,"SumOfValues","LONG") &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("OriginalLayer", "OriginalLayer_Lyr") &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("OriginalLayer_Lyr","INTERSECT",fc) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate the sum of the values in the selected features in OriginalLayer_Lyr &amp;nbsp;&amp;nbsp;&amp;nbsp; SumValues = ??&amp;nbsp; &amp;nbsp;&amp;nbsp; # Take the sum value of the line features field and populate the polygon layer's new SumOfValues field with it &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fc,"SumOfValues",SumValues)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Oct 2013 17:47:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351707#M27560</guid>
      <dc:creator>DamonOsbourne</dc:creator>
      <dc:date>2013-10-31T17:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Returning SUM of Values from a Field Within a Selection Set</title>
      <link>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351708#M27561</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Damon,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Does each feature class only have one polygon feature?&amp;nbsp; You can use a search cursor to loop through the selected lines and get a sum of a field value.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;zonePolys = arcpy.ListFeatureClasses("Zone*")&amp;nbsp; for fc in zonePolys: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(fc,"SumOfValues","DOUBLE") &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("OriginalLayer", "OriginalLayer_Lyr") &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("OriginalLayer_Lyr","INTERSECT",fc) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate the sum of the values in the selected features in OriginalLayer_Lyr &amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor("OriginalLayer_Lyr", ["Length"]) as cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sumValues = 0 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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; sumValues += row[0]&amp;nbsp; &amp;nbsp;&amp;nbsp; # Take the sum value of the line features field and populate the polygon layer's new SumOfValues field with it &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fc,"SumOfValues",sumValues)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You will just need to change the field 'Length' to the field you want to sum.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Oct 2013 18:33:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351708#M27561</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2013-10-31T18:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: Returning SUM of Values from a Field Within a Selection Set</title>
      <link>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351709#M27562</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use the sum() function. For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;zonePolys = arcpy.ListFeatureClasses("Zone*")
for fc in zonePolys:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(fc,"SumOfValues","LONG")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("OriginalLayer", "OriginalLayer_Lyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("OriginalLayer_Lyr","INTERSECT",fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; someValue = sum([r[0] for r in arcpy.da.SearchCursor("OriginalLayer_Lyr",["FIELD_NM"])]) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fc,"SumOfValues",someValue) #haha get it...&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:31:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351709#M27562</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T16:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: Returning SUM of Values from a Field Within a Selection Set</title>
      <link>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351710#M27563</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;someValue, funny!&amp;nbsp; Thanks guys it worked.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 02 Nov 2013 03:15:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-sum-of-values-from-a-field-within-a/m-p/351710#M27563</guid>
      <dc:creator>DamonOsbourne</dc:creator>
      <dc:date>2013-11-02T03:15:37Z</dc:date>
    </item>
  </channel>
</rss>

