<?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 Python Accumulate Values in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487516#M38058</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In another post, I found this python script that creates a running total.&amp;nbsp; I was able to modify it to do a running product, but I need to have the running product reset when the value of another field changes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How might I modify this code to do that?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Calculate the accumulative value of a numeric field. &lt;/LI&gt;&lt;LI&gt; &lt;/LI&gt;&lt;LI&gt;Expression: &lt;/LI&gt;&lt;LI&gt;accumulate(!FieldA!) &lt;/LI&gt;&lt;LI&gt; &lt;/LI&gt;&lt;LI&gt;Expression Type: &lt;/LI&gt;&lt;LI&gt;PYTHON_9.3 &lt;/LI&gt;&lt;LI&gt; &lt;/LI&gt;&lt;LI&gt;Code Block: &lt;/LI&gt;&lt;LI&gt;total = 0 &lt;/LI&gt;&lt;LI&gt;def accumulate(increment): &lt;/LI&gt;&lt;LI&gt;global total &lt;/LI&gt;&lt;LI&gt;if total: &lt;/LI&gt;&lt;LI&gt;&amp;nbsp; total *= increment &lt;/LI&gt;&lt;LI&gt;else: &lt;/LI&gt;&lt;LI&gt;&amp;nbsp; total = increment &lt;/LI&gt;&lt;LI&gt;return total &lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My data is similar to this&lt;/P&gt;&lt;P&gt;95&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" jive-data-cell="{&amp;quot;color&amp;quot;:&amp;quot;#3D3D3D&amp;quot;,&amp;quot;textAlign&amp;quot;:&amp;quot;left&amp;quot;,&amp;quot;padding&amp;quot;:&amp;quot;6&amp;quot;,&amp;quot;backgroundColor&amp;quot;:&amp;quot;transparent&amp;quot;,&amp;quot;fontFamily&amp;quot;:&amp;quot;Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif&amp;quot;,&amp;quot;verticalAlign&amp;quot;:&amp;quot;baseline&amp;quot;}" jive-data-header="{&amp;quot;color&amp;quot;:&amp;quot;#505050&amp;quot;,&amp;quot;backgroundColor&amp;quot;:&amp;quot;#F2F2F2&amp;quot;,&amp;quot;textAlign&amp;quot;:&amp;quot;left&amp;quot;,&amp;quot;padding&amp;quot;:&amp;quot;6&amp;quot;}" style="border: 1px solid #c6c6c6; width: 100%;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: left; background-color: #f2f2f2; color: #505050; padding: 6px;" valign="middle"&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: left; background-color: #f2f2f2; color: #505050; padding: 6px;" valign="middle"&gt;&lt;STRONG&gt;Value&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 6px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 6px;"&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 6px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 6px;"&gt;9&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 6px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 6px;"&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 6px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 6px;"&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The answer would be 1 = 81 and 2 = 25 in the 2nd row of each ID.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 15 Sep 2015 11:44:08 GMT</pubDate>
    <dc:creator>ToddHenry1</dc:creator>
    <dc:date>2015-09-15T11:44:08Z</dc:date>
    <item>
      <title>Python Accumulate Values</title>
      <link>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487516#M38058</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In another post, I found this python script that creates a running total.&amp;nbsp; I was able to modify it to do a running product, but I need to have the running product reset when the value of another field changes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How might I modify this code to do that?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Calculate the accumulative value of a numeric field. &lt;/LI&gt;&lt;LI&gt; &lt;/LI&gt;&lt;LI&gt;Expression: &lt;/LI&gt;&lt;LI&gt;accumulate(!FieldA!) &lt;/LI&gt;&lt;LI&gt; &lt;/LI&gt;&lt;LI&gt;Expression Type: &lt;/LI&gt;&lt;LI&gt;PYTHON_9.3 &lt;/LI&gt;&lt;LI&gt; &lt;/LI&gt;&lt;LI&gt;Code Block: &lt;/LI&gt;&lt;LI&gt;total = 0 &lt;/LI&gt;&lt;LI&gt;def accumulate(increment): &lt;/LI&gt;&lt;LI&gt;global total &lt;/LI&gt;&lt;LI&gt;if total: &lt;/LI&gt;&lt;LI&gt;&amp;nbsp; total *= increment &lt;/LI&gt;&lt;LI&gt;else: &lt;/LI&gt;&lt;LI&gt;&amp;nbsp; total = increment &lt;/LI&gt;&lt;LI&gt;return total &lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My data is similar to this&lt;/P&gt;&lt;P&gt;95&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" jive-data-cell="{&amp;quot;color&amp;quot;:&amp;quot;#3D3D3D&amp;quot;,&amp;quot;textAlign&amp;quot;:&amp;quot;left&amp;quot;,&amp;quot;padding&amp;quot;:&amp;quot;6&amp;quot;,&amp;quot;backgroundColor&amp;quot;:&amp;quot;transparent&amp;quot;,&amp;quot;fontFamily&amp;quot;:&amp;quot;Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif&amp;quot;,&amp;quot;verticalAlign&amp;quot;:&amp;quot;baseline&amp;quot;}" jive-data-header="{&amp;quot;color&amp;quot;:&amp;quot;#505050&amp;quot;,&amp;quot;backgroundColor&amp;quot;:&amp;quot;#F2F2F2&amp;quot;,&amp;quot;textAlign&amp;quot;:&amp;quot;left&amp;quot;,&amp;quot;padding&amp;quot;:&amp;quot;6&amp;quot;}" style="border: 1px solid #c6c6c6; width: 100%;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: left; background-color: #f2f2f2; color: #505050; padding: 6px;" valign="middle"&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: left; background-color: #f2f2f2; color: #505050; padding: 6px;" valign="middle"&gt;&lt;STRONG&gt;Value&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 6px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 6px;"&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 6px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 6px;"&gt;9&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 6px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 6px;"&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 6px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 6px;"&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The answer would be 1 = 81 and 2 = 25 in the 2nd row of each ID.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Sep 2015 11:44:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487516#M38058</guid>
      <dc:creator>ToddHenry1</dc:creator>
      <dc:date>2015-09-15T11:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Python Accumulate Values</title>
      <link>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487517#M38059</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Have you tried adding another global variable to store the value in the other field so that you can check to see if it changed? You'd also need to add another variable to the expression to pass in the id value.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Sep 2015 14:27:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487517#M38059</guid>
      <dc:creator>FreddieGibson</dc:creator>
      <dc:date>2015-09-15T14:27:08Z</dc:date>
    </item>
    <item>
      <title>Re: Python Accumulate Values</title>
      <link>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487518#M38060</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Below is the code I ended up using with some help from Tech Support.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This goes in the code block:&lt;/P&gt;&lt;P&gt;total = 0&lt;/P&gt;&lt;P&gt;LocationID = 0&lt;/P&gt;&lt;P&gt;def accumulate(locID, Inc):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; global total, LocationID&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if locID != LocationID:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; total = 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LocationID = locID&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if total: total = total*Inc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: total = Inc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return total&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the expression I use accumulate(!LocationID!,!IncField!)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The LocationID is my grouping Field and the IncField is the one that gets multiplied together.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Sep 2015 21:43:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487518#M38060</guid>
      <dc:creator>ToddHenry1</dc:creator>
      <dc:date>2015-09-23T21:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Python Accumulate Values</title>
      <link>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487519#M38061</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just like Freddie said ... for others following this&lt;/P&gt;&lt;P&gt;total&lt;/P&gt;&lt;P&gt;LocationID&lt;/P&gt;&lt;P&gt;in the posted solution are both global variables whose values get updated inside the scope of the def .&amp;nbsp; Globals...although generally not recommended... are useful should you need to persist an initial value so that it can be updated as the cursor is incremented&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Sep 2015 22:04:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-accumulate-values/m-p/487519#M38061</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-09-23T22:04:35Z</dc:date>
    </item>
  </channel>
</rss>

