<?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: Summing areas within unique values via loops in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740459#M57264</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can't you just run &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Summary_Statistics/00080000001z000000/"&gt;Summary Statistics&lt;/A&gt;, where case fields = soil type and DA value, and stat type = sum area?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 14 Apr 2015 16:05:55 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2015-04-14T16:05:55Z</dc:date>
    <item>
      <title>Summing areas within unique values via loops</title>
      <link>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740458#M57263</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is less of a python question and more of a general logic question, I suppose.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a shapefile that represents the intersection of soil types and drainage boundaries.&amp;nbsp; I want to sum the total area of each soil type within each drainage boundary.&amp;nbsp; There are 4 soil types.&amp;nbsp; There are a potentially unlimited number of drainage areas. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Soil type (HSG) field: "GRIDCODE"&lt;/P&gt;&lt;P&gt;Drainage area field: "DA"&lt;/P&gt;&lt;P&gt;Area: "Area" &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a code that extracts unique values from the drainage area field.&amp;nbsp; My problem is that I am not sure how to implement a loop sequence that iterates through each unique drainage value to find the instances of the soil types and sum the Area without manually coding each one. Earlier, I summed the areas of all the soil types with the following code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;with &lt;/SPAN&gt;arcpy.da.SearchCursor(SiteHSG, [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"gridcode"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Area"&lt;/SPAN&gt;]) &lt;SPAN style="color: #000080; font-weight: bold;"&gt;as &lt;/SPAN&gt;cursor:
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #808080; font-style: italic;"&gt;# Initialize variables
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;SiteArea_A = &lt;SPAN style="color: #0000ff;"&gt;0.000
&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;SiteArea_B = &lt;SPAN style="color: #0000ff;"&gt;0.000
&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;SiteArea_C = &lt;SPAN style="color: #0000ff;"&gt;0.000
&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;SiteArea_D = &lt;SPAN style="color: #0000ff;"&gt;0.000
&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# Calculate area for each HSG
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;row &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;cursor:
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] == &lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_A = SiteArea_A + row[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] == &lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_B = SiteArea_B + row[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] == &lt;SPAN style="color: #0000ff;"&gt;3&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_C = SiteArea_C + row[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] == &lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_D = SiteArea_D + row[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have no idea if there was a better way to calculate those areas or not and I have no problem using a similar code over and over, basically, but I do not know how to tell the computer that for each unique DA value, find soil type, and sum those soil types, then move on and search for the next unique DA value and iterate through the grid codes and sum the area. I am kind of thinking a "while" loop may be able to accomplish what I need, but then I am not really sure how to implement a while loop in this context. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am kind of thinking of something like this so far: &lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;DAvalues = [&lt;SPAN style="background-color: #e4e4ff;"&gt;row&lt;/SPAN&gt;[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] &lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;&lt;SPAN style="background-color: #ffe4ff;"&gt;row&lt;/SPAN&gt; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;arcpy.da.SearchCursor(DAHSG, DAID)]
DAunique = &lt;SPAN style="color: #000080;"&gt;set&lt;/SPAN&gt;(DAvalues)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(DAunique)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;with &lt;/SPAN&gt;arcpy.da.SearchCursor(DAHSG, ["DA", &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"gridcode"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Area"&lt;/SPAN&gt;]) &lt;SPAN style="color: #000080; font-weight: bold;"&gt;as &lt;/SPAN&gt;cursor:
for row[0] == DAunique[0]
 &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[1] == &lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_A = SiteArea_A + row[3]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[1] == &lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_B = SiteArea_B + row[3]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[1] == &lt;SPAN style="color: #0000ff;"&gt;3&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_C = SiteArea_C + row[3]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[1] == &lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_D = SiteArea_D + row[3]

for row[0] == DAunique[1]
 &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[1] == &lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_A = SiteArea_A + row[3]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[1] == &lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_B = SiteArea_B + row[3]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[1] == &lt;SPAN style="color: #0000ff;"&gt;3&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_C = SiteArea_C + row[3]
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;row[1] == &lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;:
&amp;nbsp; SiteArea_D = SiteArea_D + row[3]&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Etc.&amp;nbsp; I know there's a better way to do this than making a bunch of different for loops, especially because the number of unique values are unlimited.&amp;nbsp; It's impractical for me to code it this way but I'm not sure where to go.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:30:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740458#M57263</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2021-12-12T07:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: Summing areas within unique values via loops</title>
      <link>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740459#M57264</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can't you just run &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Summary_Statistics/00080000001z000000/"&gt;Summary Statistics&lt;/A&gt;, where case fields = soil type and DA value, and stat type = sum area?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Apr 2015 16:05:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740459#M57264</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-04-14T16:05:55Z</dc:date>
    </item>
    <item>
      <title>Re: Summing areas within unique values via loops</title>
      <link>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740460#M57265</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ohmygod. I have been racking my brain trying to figure out how to loop this thing and there's a built in function that does it for me??&amp;nbsp; I was toying with converting to raster and using combine/zonal stats if all else failed.&amp;nbsp; It should have occurred to me to check for a similar shapefile function.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you! I'll see if this gives the output I desire.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Apr 2015 16:09:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740460#M57265</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-14T16:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: Summing areas within unique values via loops</title>
      <link>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740461#M57266</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Whoo it worked! And I can use SearchCursor to pull out the values so I can use them in my program.&amp;nbsp; Awesome.&amp;nbsp; Thanks a ton!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Apr 2015 16:32:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740461#M57266</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-14T16:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Summing areas within unique values via loops</title>
      <link>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740462#M57267</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Alternatively, you can harness the da.FeatureClassToNumPyArray() and vice-versa to perform grouping outside of ESRI stack.&amp;nbsp; This works well if say your Intersect output has millions of rows and efficiency is important.&amp;nbsp; There's some other benefits that might not apply to this scenario, but just thought it good to point out that numpy and pandas libraries offer some excellent alternatives.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This would produce your sum of the area by gridcode value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import numpy
import pandas as pd
from pandas import *

fc = r'&amp;lt;your feature class&amp;gt;'

#create a NumPy array from the input feature class&amp;nbsp; 
nparr = arcpy.da.FeatureClassToNumPyArray(fc, ['OBJECTID','AREA','GRIDCODE'])&amp;nbsp; 
&amp;nbsp; 
#create a pandas DataFrame object from the NumPy array&amp;nbsp; 
df = DataFrame(nparr, columns=['OBJECTID','AREA','GRIDCODE'])
df1 = df.groupby(['GRIDCODE']).sum()
print df1

#we are just printing results, but could also go back to ESRI stack with arcpy.da.NumPyArrayToFeatureClass()

sys.exit()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:30:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/summing-areas-within-unique-values-via-loops/m-p/740462#M57267</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-12T07:30:52Z</dc:date>
    </item>
  </channel>
</rss>

