<?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 script to iterate through feature and select each category in a field. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285780#M22078</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a category field with 9 categories, what's an easy method to go about selecting 1 of each category?&amp;nbsp; It can be random features, I just need one of each category.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 23 Dec 2014 17:55:03 GMT</pubDate>
    <dc:creator>GeoffreyWest</dc:creator>
    <dc:date>2014-12-23T17:55:03Z</dc:date>
    <item>
      <title>Python script to iterate through feature and select each category in a field.</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285780#M22078</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a category field with 9 categories, what's an easy method to go about selecting 1 of each category?&amp;nbsp; It can be random features, I just need one of each category.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Dec 2014 17:55:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285780#M22078</guid>
      <dc:creator>GeoffreyWest</dc:creator>
      <dc:date>2014-12-23T17:55:03Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to iterate through feature and select each category in a field.</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285781#M22079</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Geoffrey,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can iterate through the Category field and append the value and corresponding OBJECTID to a dictionary.&amp;nbsp; Then, remove duplicate values from the dictionary leaving only one OBJECTID for each category.&amp;nbsp; You can then perform the selection based on the dictionary key.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
env.overwriteOutput = 1
env.workspace = r"C:\temp\python\test.gdb"

fc = "line"

dict = {}

with arcpy.da.SearchCursor(fc, ["OBJECTID", "Category"]) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dict[row[0]] = row[1]

del cursor

dict2 = {}

#remove duplicate values from dictionary
for key,value in dict.items():
&amp;nbsp;&amp;nbsp;&amp;nbsp; if value not in dict2.values():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dict2[key] = value

arcpy.MakeFeatureLayer_management(fc, "fcLyr")

#perform selection based on dictionary key
for key in dict2:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("fcLyr", "NEW_SELECTION", "OBJECTID = " + str(key))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("fcLyr", "fc_" + str(key))

print 'finished'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:48:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285781#M22079</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T13:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to iterate through feature and select each category in a field.</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285782#M22080</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would think that to select one of each Category all at once in the layer you would want the code to be like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;import&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; arcpy&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="comment" style="font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5; color: #008200; font-size: 9pt !important;"&gt;"""Get the current map layers."""&lt;/SPAN&gt;&lt;SPAN style="color: black; font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5; font-size: 9pt !important;"&gt;&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="color: black; font-size: 9pt; font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5;"&gt;mxd = arcpy.mapping.MapDocument(&lt;/SPAN&gt;&lt;SPAN class="string" style="font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5; color: blue; font-size: 9pt !important;"&gt;"CURRENT"&lt;/SPAN&gt;&lt;SPAN style="color: black; font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5; font-size: 9pt !important;"&gt;)&amp;nbsp; &lt;/SPAN&gt;

&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN style="color: black; font-size: 9pt; font-style: inherit; font-weight: inherit;"&gt;lyrName = &lt;/SPAN&gt;&lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;"line"&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;

&lt;SPAN class="comment" style="font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5; color: #008200; font-size: 9pt !important;"&gt;"""Find the layer name"""&lt;/SPAN&gt;&lt;SPAN style="color: black; font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5; font-size: 9pt !important;"&gt;&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="color: black; font-size: 9pt; font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5;"&gt;lyr = arcpy.mapping.ListLayers(mxd, lyrName)[&lt;/SPAN&gt;&lt;SPAN class="number" style="font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5; color: green; font-size: 9pt !important;"&gt;0&lt;/SPAN&gt;&lt;SPAN style="color: black; font-style: inherit; font-weight: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 1.5; font-size: 9pt !important;"&gt;]&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="color: black; font-size: 9pt; font-style: inherit; font-weight: inherit; line-height: 1.5;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;dict = {}&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;with arcpy.da.SearchCursor(fc, [&lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;"Category", &lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;"OID@"&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;]) as cursor:&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;for&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; row &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; cursor:&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-size: 9pt !important; font-style: inherit; background-color: inherit; font-weight: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keyValue = row[0]&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-size: 9pt !important; font-style: inherit; background-color: inherit; font-weight: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #006699; background-color: #f6f6f6;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #000000; background-color: #f6f6f6;"&gt; &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #006699; background-color: #f6f6f6;"&gt;not&lt;/SPAN&gt;&lt;SPAN style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #000000; background-color: #f6f6f6;"&gt; keyValue &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #006699; background-color: #f6f6f6;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #000000; background-color: #f6f6f6;"&gt; valueDict:&lt;/SPAN&gt; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dict[keyValue&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;] = row[&lt;/SPAN&gt;&lt;SPAN class="number" style="font-weight: inherit; font-style: inherit; color: green; font-size: 9pt !important; background-color: inherit;"&gt;1&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;]&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;del&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; cursor&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;

&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;#perform selection based on one ObjecctID value from each Category&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;whereClause = "&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;OBJECTID IN &lt;/SPAN&gt;("&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;for&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; key &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; dict:&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-size: 9pt !important; font-style: inherit; background-color: inherit; font-weight: inherit;"&gt;&amp;nbsp;&amp;nbsp; if whereClause == "OBJECTID IN (":&lt;/SPAN&gt;
&lt;SPAN style="color: black; font-size: 9pt !important; font-style: inherit; background-color: inherit; font-weight: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = whereClause + str(dict(key))&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp; else:&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000000; font-size: 12px;"&gt;whereClause = whereClause + ", " + str(dict(key))&lt;/SPAN&gt;&lt;/SPAN&gt;

&lt;SPAN style="color: #000000; font-style: inherit; font-size: 12px; background-color: inherit; font-weight: inherit;"&gt;whereClause = whereClause + ")"&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN style="color: #000000;"&gt;arcpy.SelectLayerByAttribute_management(&lt;/SPAN&gt;&lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;lyrName&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;, &lt;/SPAN&gt;&lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;"NEW_SELECTION"&lt;/SPAN&gt;&lt;SPAN style="color: #000000; font-style: inherit; font-size: 9pt !important; background-color: inherit; font-weight: inherit;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;whereClause)&lt;/SPAN&gt;&lt;/SPAN&gt;

&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;print&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; &lt;/SPAN&gt;&lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;'finished'&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:48:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285782#M22080</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T13:48:47Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to iterate through feature and select each category in a field.</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285783#M22081</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The "easiest" being subjective to each person, I've really enjoyed and like to utilize non-ESRI libraries for this kind of grouping.&amp;nbsp; Specifically, numpy and pandas are excellent at doing these kinds of operations and would allow you to determine unique values that you could then use to make additional selections or other useful tasks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import numpy
from pandas import *

ws = r"H:\Documents\ArcGIS\Default.gdb"
fc = ws + "&lt;A href="https://community.esri.com/" rel="nofollow noopener noreferrer" target="_blank"&gt;\\MyFeatureClass&lt;/A&gt;"

#create a NumPy array from the input feature class
nparr = arcpy.da.FeatureClassToNumPyArray(fc, '*')

#create a pandas DataFrame object from the NumPy array
df = DataFrame(nparr, columns=['ObjectId', 'Layer', 'Row', 'Col'])

#access unique values for the field
uniqueValues = numpy.unique(df['Layer'])
for uniqueValue in uniqueValues:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print uniqueValue&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:48:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-iterate-through-feature-and/m-p/285783#M22081</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T13:48:50Z</dc:date>
    </item>
  </channel>
</rss>

