<?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: Calculate Field with arcpy in Python Snippets Questions</title>
    <link>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802853#M272</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for this answer, Joshua Bixby. I will try it! &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 01 Oct 2014 17:16:58 GMT</pubDate>
    <dc:creator>JuttaSchiller</dc:creator>
    <dc:date>2014-10-01T17:16:58Z</dc:date>
    <item>
      <title>Calculate Field with arcpy</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802847#M266</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;At first I made a statistic analyst to get the sum of the field area for equal Ids.Now I need the sum of all area fields from the output-table therefore I did another statistic analyst to get the sum. With a searchcursor I tried to get the sumvalue to put it in the calculate field tool. But in the end it is not calculating the percent, so the field I add before and I don’t know what I did wrong. I need to do it with arcpy because it is a small part of a big tool I’m trying to build and the sumvalue will change from input to input.&lt;/P&gt;&lt;P&gt;It would be wonderful if you can tell me my mistake.&lt;/P&gt;&lt;P&gt;The following code is working, but there is just a 0 in the percent field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;import os&lt;/P&gt;
&lt;P&gt;from arcpy import env&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;env.overwriteOutput = True&lt;/P&gt;
&lt;P&gt;env.workspace = r"D:\Users\jul\ers\cities_UA\resultfolder"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;inputshp = r"D:\Users\jul\ers\cities_UA\resultfolder\at005l_innsbruck_result.shp"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;outtable= r"D:\Users\jul\ers\cities_UA\resultfolder\outtable.dbf"&lt;/P&gt;
&lt;P&gt;arcpy.Statistics_analysis(inputshp, outtable, [["area", "SUM"]], "Id")&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;outtableSUM= r"D:\Users\jul\ers\cities_UA\resultfolder\outtableSUM.dbf"&lt;/P&gt;
&lt;P&gt;arcpy.Statistics_analysis(outtable, outtableSUM, [["SUM_area", "SUM"]])&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;with arcpy.da.SearchCursor(outtableSUM, "SUM_SUM_ar") as cursor:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print (int(row[0]))&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;fieldsum = (int(row[0]))&lt;/P&gt;
&lt;P&gt;print fieldsum&lt;/P&gt;
&lt;P&gt;arcpy.AddField_management(outtable, "prozent", "DOUBLE")#working&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;arcpy.CalculateField_management(outtable, "prozent", "([SUM_area]*100)/fieldsum", "VB")&lt;/P&gt;

&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Sep 2014 13:14:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802847#M266</guid>
      <dc:creator>JuttaSchiller</dc:creator>
      <dc:date>2014-09-26T13:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with arcpy</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802848#M267</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jutta,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;with arcpy.da.SearchCursor(outtableSUM, "SUM_SUM_ar") as cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print (int(row[0]))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;fieldsum = (int(row[0]))&lt;/P&gt;&lt;P&gt;print fieldsum&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;you are setting the value of fieldsum after the cursor finishes running.&amp;nbsp; Are you trying to sum up the values in "SUM_SUM_ar"? if so, you need to make it a variable prior to the cursor then add the value of int(row[0]) to it each time&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;fieldsum = 0&lt;/P&gt;&lt;P&gt;with arcpy.da.SearchCursor(outtableSUM, "SUM_SUM_ar") as cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldsum+= int(row[0]))&lt;/P&gt;&lt;P&gt;print fieldsum&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Sep 2014 20:10:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802848#M267</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2014-09-26T20:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with arcpy</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802849#M268</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey everybody: &lt;A href="https://community.esri.com/migration-blogpost/1070"&gt;Posting Code blocks in the new GeoNet&lt;/A&gt;‌&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jutta, Ian is right, let me put it another way: you need to pack the sum you've calculated into the calculate expression string like this. I added spaces to your expression for readability. They are not required, it's a style thing.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14117894721458411" jivemacro_uid="_14117894721458411" modifiedtitle="true"&gt;
&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;arcpy.CalculateField_management(outtable, "prozent",&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "( [SUM_ar&lt;/SPAN&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;ea] * 100 ) / {}".format(fieldsum)&lt;/SPAN&gt;&lt;/P&gt;



&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A shortcoming here is that you are using a folder workspace so everything writes to shapefile/dbf which truncates all your field names (and there are &lt;A href="http://resources.arcgis.com/en/help/main/10.2/0056/005600000013000000.htm"&gt;other limitations&lt;/A&gt; too). I highly recommend moving to file gdb, or try the in_memory gdb (very fast - a good choice for small tables like this).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another piece of advice, always use PYTHON or PYTHON_9.3 with Calculate_Field so your scripts will work in background GP or maybe someday in ArcGIS Pro. (Calculate Field does not support the default VB parser in x64 arcpy.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I always use upper case field names in my code (even though they are case-insensitive) because... INFO.&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/cool.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14117890665266724" jivemacro_uid="_14117890665266724" modifiedtitle="true"&gt;
&lt;P&gt;tmp1 = "in_memory/xxsum1"&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="line-height: 1.5;"&gt;# sum area by ID&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;arcpy.Statistics_analysis(inputshp, tmp1, [["AREA", "SUM"]], "ID")&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;# sum SUM_AREA - using arcpy.da cursor (nice plan, Ian!)&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;fieldsum = 0&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;with arcpy.da.SearchCursor(tmp1, "SUM_AREA") as cursor:&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldsum += row[0]&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;# create percent field and populate it&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;arcpy.AddField_management(tmp1, "PROZENT", "DOUBLE")&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;expr = "100.0 *&amp;nbsp; !SUM_AREA! / {}".format(fieldsum)&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;arcpy.CalculateField_management(tmp1, "PROZENT", expr, "PYTHON")&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;arcpy.CopyRows_management(tmp1, out_table)&lt;/P&gt;
&lt;P style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;arcpy.Delete_management(tmp1)&lt;/P&gt;







&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 27 Sep 2014 03:39:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802849#M268</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-09-27T03:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with arcpy</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802850#M269</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ian and Curtis Price,&lt;/P&gt;&lt;P&gt;thanks so much for your answers and advices. I will try them on Monday because I do not have ArcGIS 10.2 at home.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 27 Sep 2014 11:42:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802850#M269</guid>
      <dc:creator>JuttaSchiller</dc:creator>
      <dc:date>2014-09-27T11:42:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with arcpy</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802851#M270</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you so much. It works perfectly. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Sep 2014 07:51:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802851#M270</guid>
      <dc:creator>JuttaSchiller</dc:creator>
      <dc:date>2014-09-29T07:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with arcpy</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802852#M271</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;When doing Python scripting in ArcGIS, I tend to favor native ArcPy methods over geoprocessing tools.&amp;nbsp; The geoprocessing tools are convenient, but that convenience can come with a performance hit, and the hit can be significant with some tools.&amp;nbsp; Even within the ArcPy realm, I have found methods in the data access module (da) noticeably more efficient than corresponding methods in the base site package (arcpy).&amp;nbsp; (&lt;EM&gt;I think this has been one of the main selling points of the da module from the beginning&lt;/EM&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

# import functions from modules that are available but not commonly imported


from collections import defaultdict
from numpy import fromiter, dtype


# sum area by ID


stats = defaultdict(int)
with arcpy.da.SearchCursor(inputshp, ['ID','AREA']) as cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for k, v in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stats&lt;K&gt; += v&lt;/K&gt;


# sum SUM_AREA - using sum over an iterable of dict's values
fieldsum = sum(stats.itervalues())


# create iterable and populate numpy array


stats_iterable = ((k, v, 100.0 * v / fieldsum) for (k, v) in stats.iteritems())
tmp1 = fromiter(stats_iterable,
&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; dtype([('ID', 'i4'), ('SUM_AREA', 'f8'), ('PROZENT', 'f8')]))


# Dump numpy array to table


arcpy.da.NumPyArrayToTable(tmp1, out_table)


del tmp1


del stats


....


&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The code above only uses 2 arcpy functions, both of which are in the data access module.&amp;nbsp; On a randomly generated million-record test data set, cutting out most of the arcpy functions reduced the runtime by more than 75%.&amp;nbsp; Profiling the original code shows the bulk of the extra runtime comes from a single function: analysis.py:(Statistics).&amp;nbsp; I have seen this numerous times, i.e., scripts that call &lt;SPAN style="color: #4d4d4d; text-indent: 0px;"&gt;Statistics_analysis&lt;/SPAN&gt; on large data sets get really bogged down.&amp;nbsp; I am not sure how large your data sets are, but if they are large and you want to speed them up, rolling your own functions can usually get you performance gains.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 09:20:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802852#M271</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-12T09:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with arcpy</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802853#M272</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for this answer, Joshua Bixby. I will try it! &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2014 17:16:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-field-with-arcpy/m-p/802853#M272</guid>
      <dc:creator>JuttaSchiller</dc:creator>
      <dc:date>2014-10-01T17:16:58Z</dc:date>
    </item>
  </channel>
</rss>

