<?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: Random Polygon Selector by Attribute/Area in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/random-polygon-selector-by-attribute-area/m-p/49728#M3910</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here is the code to populate an attribute in the attribute table with a 0 or 1 for 5% of your records.&amp;nbsp; If you want to select 5% of those records that have some other attribute value, then just insert a &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;select by attribute &lt;/SPAN&gt;&lt;SPAN&gt; where I have it marked, then only 5% of the selected records will be assigned a 1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''Selects a random number of records from a feature class and populates a
&amp;nbsp;&amp;nbsp; user-defined field with either a 1 or a 0 based on the input parameters.&amp;nbsp; For
&amp;nbsp;&amp;nbsp; example, if percent_to_include is set to 5, then a random ~5 percent of the
&amp;nbsp;&amp;nbsp; records will be asigned a value of 1, the rest a value of 0.
'''
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; import operator, os, random, traceback, sys, arcpy

&amp;nbsp;&amp;nbsp;&amp;nbsp; ##Path to the feature class.
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = r"Z:\GISpublic\GerryG\RestorationandMitigationConcept\20110425IvyPlotMonitoring\transects.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; ##The attribute to populate with 1 or 0...
&amp;nbsp;&amp;nbsp;&amp;nbsp; attribute = r"issample"
&amp;nbsp;&amp;nbsp;&amp;nbsp; ##The percent threshold to include as a random selection.
&amp;nbsp;&amp;nbsp;&amp;nbsp; percent_to_include = 5

&amp;nbsp;&amp;nbsp;&amp;nbsp; ##INSERT YOUR SELECT BY ATTRIBIUTE HERE

&amp;nbsp;&amp;nbsp;&amp;nbsp; uc = arcpy.UpdateCursor(fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in uc:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = random.random() * 100
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.AddMessage(x)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x &amp;lt;= percent_to_include:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(attribute, 1)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(attribute, 0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; uc.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Done"
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; tb = sys.exc_info()[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(tb)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; print pymsg + "\n"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:55:21 GMT</pubDate>
    <dc:creator>GerryGabrisch</dc:creator>
    <dc:date>2021-12-10T21:55:21Z</dc:date>
    <item>
      <title>Random Polygon Selector by Attribute/Area</title>
      <link>https://community.esri.com/t5/python-questions/random-polygon-selector-by-attribute-area/m-p/49727#M3909</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am very new to Python scripting and am trying to create a tool to randomly select a sub set of polygons by an attribute and equaling ~ 10% of the total attribute area.&amp;nbsp; It seems that this shouldn't be so difficult but it is causing me a lot of problems.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please help.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Jul 2011 15:36:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/random-polygon-selector-by-attribute-area/m-p/49727#M3909</guid>
      <dc:creator>RustyGriffin</dc:creator>
      <dc:date>2011-07-22T15:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: Random Polygon Selector by Attribute/Area</title>
      <link>https://community.esri.com/t5/python-questions/random-polygon-selector-by-attribute-area/m-p/49728#M3910</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here is the code to populate an attribute in the attribute table with a 0 or 1 for 5% of your records.&amp;nbsp; If you want to select 5% of those records that have some other attribute value, then just insert a &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;select by attribute &lt;/SPAN&gt;&lt;SPAN&gt; where I have it marked, then only 5% of the selected records will be assigned a 1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''Selects a random number of records from a feature class and populates a
&amp;nbsp;&amp;nbsp; user-defined field with either a 1 or a 0 based on the input parameters.&amp;nbsp; For
&amp;nbsp;&amp;nbsp; example, if percent_to_include is set to 5, then a random ~5 percent of the
&amp;nbsp;&amp;nbsp; records will be asigned a value of 1, the rest a value of 0.
'''
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; import operator, os, random, traceback, sys, arcpy

&amp;nbsp;&amp;nbsp;&amp;nbsp; ##Path to the feature class.
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = r"Z:\GISpublic\GerryG\RestorationandMitigationConcept\20110425IvyPlotMonitoring\transects.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; ##The attribute to populate with 1 or 0...
&amp;nbsp;&amp;nbsp;&amp;nbsp; attribute = r"issample"
&amp;nbsp;&amp;nbsp;&amp;nbsp; ##The percent threshold to include as a random selection.
&amp;nbsp;&amp;nbsp;&amp;nbsp; percent_to_include = 5

&amp;nbsp;&amp;nbsp;&amp;nbsp; ##INSERT YOUR SELECT BY ATTRIBIUTE HERE

&amp;nbsp;&amp;nbsp;&amp;nbsp; uc = arcpy.UpdateCursor(fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in uc:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = random.random() * 100
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.AddMessage(x)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x &amp;lt;= percent_to_include:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(attribute, 1)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(attribute, 0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; uc.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Done"
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; tb = sys.exc_info()[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(tb)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; print pymsg + "\n"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:55:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/random-polygon-selector-by-attribute-area/m-p/49728#M3910</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2021-12-10T21:55:21Z</dc:date>
    </item>
  </channel>
</rss>

