<?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: Counting instances of non-zero fields in a record in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388007#M30650</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Numpy has a direct solution, so get your data into an array either using tabletonumpyarray or featureclasstonumpyarray or whatever&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a
array([[&amp;nbsp; 0,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2013,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2015],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2011, 2112, 2013,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2015],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2011,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2014, 2015],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2011,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2013, 2014,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0]])
&amp;gt;&amp;gt;&amp;gt; np.sum(np.where(a !=0, 1,0), axis=1)
array([2, 4, 3, 3])&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 17:49:11 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2021-12-11T17:49:11Z</dc:date>
    <item>
      <title>Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388006#M30649</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;IMG alt="arcgis_table_snip.JPG" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/213935_arcgis_table_snip.JPG" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;Referring to the table snippet above, each record has five fields for the last five fiscal years. The data will be either the number of that year or zero.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to calculate the Burns_5Yrs field based on the count of the number of non-zero years (in fields FY11 - FY15) in that record. The first three rows have examples manually entered. For example, the first record has activity in FY11 and FY14 so the result for Burns_5Yrs should be 2. The second row is also 2 and the third row is 4.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've been doing this manually but now I have a table with 198 records so I'd like a programming solution, preferably in Python. Any help would be appreciated as I really don't know where to start.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jul 2016 18:49:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388006#M30649</guid>
      <dc:creator>DavidFox</dc:creator>
      <dc:date>2016-07-29T18:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388007#M30650</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Numpy has a direct solution, so get your data into an array either using tabletonumpyarray or featureclasstonumpyarray or whatever&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a
array([[&amp;nbsp; 0,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2013,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2015],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2011, 2112, 2013,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2015],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2011,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2014, 2015],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2011,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, 2013, 2014,&amp;nbsp;&amp;nbsp;&amp;nbsp; 0]])
&amp;gt;&amp;gt;&amp;gt; np.sum(np.where(a !=0, 1,0), axis=1)
array([2, 4, 3, 3])&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:49:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388007#M30650</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T17:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388008#M30651</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;David... marking threads as discussions rather than questions, if it is indeed a question, really limits your audience to those that view everything or just questions.&amp;nbsp; With the addition of Ideas, you have to limit your viewing to everything or one of the litany of options, with many selecting questions&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jul 2016 21:18:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388008#M30651</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-07-29T21:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388009#M30652</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can use an update cursor for this:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; fc = 'points_test' # feature layer
... fields = ['FY11','FY12','FY13','FY14','FY15','Burns_5Yrs'] # your fields, with burns last
... with arcpy.da.UpdateCursor(fc,fields) as cursor: # create cursor
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor: # loop through features
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[-1] = sum(x &amp;gt; 0 for x in row[:-1]) # sum non-zeros, place in last position
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row) # write new value, after changing burns value&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:49:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388009#M30652</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T17:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388010#M30653</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can also use the field calculator:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Parser = Python&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Expression:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;blah( !FY11! , !FY12! , !FY13! , !FY14! , !FY15! )&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Codeblock:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def blah(f1,f2,f3,f4,f5):
&amp;nbsp; params = locals() # dictionary of all arguments in function 
&amp;nbsp; my_sum = 0 # cumulative total
&amp;nbsp; for k,v in params.iteritems(): # loop through dictionary
&amp;nbsp;&amp;nbsp;&amp;nbsp; if v&amp;gt;0: # inspect value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; my_sum += 1 # add to cumulative total
&amp;nbsp; return my_sum # return total&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:49:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388010#M30653</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T17:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388011#M30654</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;don't do a VB sample or I will do one in Pandas &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/silly.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jul 2016 21:55:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388011#M30654</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-07-29T21:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388012#M30655</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What the heck...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Parser = VB Script&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Expression:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;result&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Codeblock:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;result = 0
if [FY11] &amp;gt; 0 then
result = result + 1
end if
if [FY12] &amp;gt; 0 then
result = result + 1
end if
if [FY13] &amp;gt; 0 then
result = result + 1
end if
if [FY14] &amp;gt; 0 then
result = result + 1
end if
if [FY15] &amp;gt; 0 then
result = result + 1
end if&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:49:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388012#M30655</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T17:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388013#M30656</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;smartie &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/laugh.png" /&gt; now I have to find something other than an iPad poolside&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jul 2016 22:11:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388013#M30656</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-07-29T22:11:26Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388014#M30657</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Don't spill your margarita in the laptop. &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jul 2016 22:38:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388014#M30657</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2016-07-29T22:38:24Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388015#M30658</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This might simplify the field calculator code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def blah(f1,f2,f3,f4,f5): 
&amp;nbsp;&amp;nbsp;&amp;nbsp; params = locals() # dictionary of all arguments in function
&amp;nbsp;&amp;nbsp;&amp;nbsp; return sum( v &amp;gt; 0 for k, v in params.iteritems())&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or just:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def blah(f1,f2,f3,f4,f5):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return sum( v &amp;gt; 0 for k, v in locals().iteritems())&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I like the locals() function.&amp;nbsp; Thanks Darren.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:49:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388015#M30658</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2021-12-11T17:49:23Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388016#M30659</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Pandas was too clunky, so numpy in the field calculator... with pictures&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Field_calc_numpy_01.png" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/214000_Field_calc_numpy_01.png" style="width: 620px; height: 369px;" /&gt;&lt;/P&gt;&lt;P&gt;The only thing to remember&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;always choose the python parser... and there is no way to set it to default&lt;/LI&gt;&lt;LI&gt;write a function... doesn't need to be fancy.&lt;/LI&gt;&lt;LI&gt;any imports are done once, so if numpy is already imported, it is only imported once&lt;/LI&gt;&lt;LI&gt;the function just contains the variables which will be used.&amp;nbsp; In this example, I used the field names without the ! marks.&amp;nbsp; There is no requirement for the variables to have the same field names&lt;/LI&gt;&lt;LI&gt;in the expression box, you put function name (ie 'r') plus the variables to pass.&amp;nbsp; The field names are being passed so enclose them in ! marks.&lt;/LI&gt;&lt;LI&gt;save your function for later use if you like&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;That's all&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 30 Jul 2016 11:14:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388016#M30659</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-07-30T11:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388017#M30660</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The code block can be bypassed completely by using the &lt;SPAN style="font-family: courier new,courier;"&gt;sum()&lt;/SPAN&gt; function directly in the expression block:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_1469892391782549 jive_text_macro" data-renderedposition="34.959999084472656_8_1009_16" jivemacro_uid="_1469892391782549"&gt;&lt;P&gt;sum(1 for field in (!FY11!, !FY12!, !FY13!, !FY14!, !FY15!) if field)&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 30 Jul 2016 15:26:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388017#M30660</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-07-30T15:26:54Z</dc:date>
    </item>
    <item>
      <title>Re: Counting instances of non-zero fields in a record</title>
      <link>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388018#M30661</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, this worked perfectly (after I used the actual field names instead of the aliases).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Aug 2016 19:11:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-instances-of-non-zero-fields-in-a-record/m-p/388018#M30661</guid>
      <dc:creator>DavidFox</dc:creator>
      <dc:date>2016-08-24T19:11:39Z</dc:date>
    </item>
  </channel>
</rss>

