<?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: Python Script to Identify Last of each group in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285320#M22024</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Something like this should work&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":face_with_open_mouth:"&gt;😮&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;f __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; inPts = r"testLast.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = arcpy.GetCount_management(inPts).getOutput(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow = int(count) - 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lastRow
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(inPts, "last", "SHORT","","", 3)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(inPts)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue("FID")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue("FID") == lastRow:
&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; print row.getValue("FID")
&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; print "Found it"
&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; row.last = lastRow ##Put what you like here I like 1's or 0's but used last row num for testing
&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; rows.updateRow(row)
&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; print "done"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print traceback.print_exc()
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 13:47:51 GMT</pubDate>
    <dc:creator>LindseyWood</dc:creator>
    <dc:date>2021-12-11T13:47:51Z</dc:date>
    <item>
      <title>Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285319#M22023</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a shapefile of points which is GPS data from a number of collared animals.&amp;nbsp;&amp;nbsp; I am trying to write a script to add a column and flag the last entry for each animal so that it can be easily queried and displayed on the map.&amp;nbsp;&amp;nbsp; It seems like the script is working properly according to the debug results, but it does not save the results to the table.&amp;nbsp;&amp;nbsp;&amp;nbsp; I have done quite a bit of scripting with VBA but this is my first attempt at python.&amp;nbsp;&amp;nbsp;&amp;nbsp; See Script Below:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import types&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcgisscripting&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#get Parameters from toolbox&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#-For Use with Script Toolbox&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#inPts = arcpy.getparameterastext(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#Animalfield=arcpy.getparameterastext(1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#SortField=arcpy.getparameterastext(2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#-For Troubleshooting&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inPts = "C:\ArcGIS\Temp\Tester.shp"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;AnimalField = "AnimalNum"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SortField = "MSTTime"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#Add Field&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;print "Beginning:"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(inPts,"LAST_PNT","TEXT",)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ThesortString = AnimalField + " A; " + SortField + " A"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Apply Sort&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#firstTime=True&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp=arcgisscripting.create()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = gp.UpdateCursor(inPts,"","","",ThesortString)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print "Finished Sorting"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;row = rows.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;now = row.GetValue(AnimalField)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if row:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("LAST_PNT","FALSE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.UpdateRow(row)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row=rows.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;while row :&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; previous=now&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; now = row.GetValue(AnimalField)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if now == previous:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("LAST_PNT","FALSE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getvalue("LAST_PNT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else :&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("LAST_PNT","TRUE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getvalue("LAST_PNT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.UpdateRow(row)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del row, rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;print "Completed!"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Jun 2013 16:02:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285319#M22023</guid>
      <dc:creator>KarlCox</dc:creator>
      <dc:date>2013-06-03T16:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285320#M22024</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Something like this should work&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":face_with_open_mouth:"&gt;😮&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;f __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; inPts = r"testLast.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = arcpy.GetCount_management(inPts).getOutput(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow = int(count) - 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lastRow
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(inPts, "last", "SHORT","","", 3)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(inPts)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue("FID")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue("FID") == lastRow:
&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; print row.getValue("FID")
&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; print "Found it"
&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; row.last = lastRow ##Put what you like here I like 1's or 0's but used last row num for testing
&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; rows.updateRow(row)
&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; print "done"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print traceback.print_exc()
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:47:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285320#M22024</guid>
      <dc:creator>LindseyWood</dc:creator>
      <dc:date>2021-12-11T13:47:51Z</dc:date>
    </item>
    <item>
      <title>Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285321#M22025</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks,&amp;nbsp;&amp;nbsp; your script didn't quite work for me but it got me to the point where I could get it working...The script below works for me...but there is now a performance issue.&amp;nbsp;&amp;nbsp; This seems to take much longer than it should to process.&amp;nbsp;&amp;nbsp; It probably takes about 3-4 minutes to process only 1000 records.&amp;nbsp;&amp;nbsp; I have similar functions in Excel that can do this in a few seconds.&amp;nbsp; Given that my actual dataset has about 20,000+ records, I don't know how useful this script will really be.&amp;nbsp;&amp;nbsp; Does anybody have any ideas on how to speed this up?&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os
import types
from arcpy import env
#get Parameters from toolbox

#-For Use with Script Toolbox
#inPts = arcpy.getparameterastext(0)
#Animalfield=arcpy.getparameterastext(1)
#SortField=arcpy.getparameterastext(2)

#-For Troubleshooting
inPts = "C:\ArcGIS\Temp\Tester.shp"
AnimalField = "AnimalNum"
SortField = "MSTTime"

#Add Field
print "Beginning:"
arcpy.AddField_management(inPts,"LAST_PNT","TEXT",)

ThesortString = AnimalField + " A; " + SortField + " D"

# Apply Sort
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
rows = arcpy.UpdateCursor(inPts,"","","",ThesortString)
print "Finished Sorting"

thecount = arcpy.GetCount_management(inPts).getOutput(0)
i=0
lastRow = "Starter"
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print i
&amp;nbsp;&amp;nbsp;&amp;nbsp; i=i+1
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print str(count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #thecount = thecount - 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(AnimalField) == lastRow:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("LAST_PNT","FALSE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&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; row.setValue("LAST_PNT","TRUE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)

del row
del rows
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:47:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285321#M22025</guid>
      <dc:creator>KarlCox</dc:creator>
      <dc:date>2021-12-11T13:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285322#M22026</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Can use a sort field in the Cursors.&amp;nbsp; Below sorts by OID (so the table order is maintained) then sets the variable equal to the last or first table ojbect, depending on which you use.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Handy for grabbing fist/last or max/min (just change sort field) values in a table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lastValue = arcpy.SearchCursor(infc, "", "", "", OID + " D").next().getValue(myField) #Get 1st row in cursor - gets last value based on OID

firstValue = arcpy.SearchCursor(infc, "", "", "", OID + " A").next().getValue(myField) #Get last row in cursor - gets first value based on OID&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:47:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285322#M22026</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-11T13:47:56Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285323#M22027</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Does anybody have any ideas on how to speed this up? &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You're doing an updateRow when you don't have to; it would be more efficient to start by initilizing all rows to "FALSE" first and dropping your else clause. This still uses a cursor behind the scenes but it runs through quickly as there is no sort.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
arcpy.AddField_management(inPts,"LAST_PNT","TEXT",)
arcpy.CalculateField_management(inPts,"LAST_PNT", "'TRUE'", "PYTHON")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you're using ArcGIS 10.1, there is a new cursor function available that is 10x-100x faster: &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000" rel="nofollow noopener noreferrer" target="_blank"&gt;arcpy.da.SearchCursor.&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A whole different approach would be to locate your points of interest using the Summary Statistics tool instead of using a sorted cursor. You could then get the Animal ID and max time in the results, join to your point shapefile and tag using Calculate Field. If you're using 10.0, I recommend that approach.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;By the way, please [thread=48475]use code tags[/thread] so we can see your Python code!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:47:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285323#M22027</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T13:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285324#M22028</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Does anybody have any ideas on how to speed this up?&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os
import types
from arcpy import env
#get Parameters from toolbox

#-For Use with Script Toolbox
#inPts = arcpy.getparameterastext(0)
#Animalfield=arcpy.getparameterastext(1)
#SortField=arcpy.getparameterastext(2)

#-For Troubleshooting
inPts = "C:\ArcGIS\Temp\Tester.shp"
AnimalField = "AnimalNum"
SortField = "MSTTime"

#Add Field
print "Beginning:"
arcpy.AddField_management(inPts,"LAST_PNT","TEXT",)

ThesortString = AnimalField + " A; " + SortField + " D"

# Apply Sort
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
rows = arcpy.UpdateCursor(inPts,"","","",ThesortString)
print "Finished Sorting"

thecount = arcpy.GetCount_management(inPts).getOutput(0)
i=0
lastRow = "Starter"
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print i
&amp;nbsp;&amp;nbsp;&amp;nbsp; i=i+1
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print str(count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #thecount = thecount - 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(AnimalField) == lastRow:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("LAST_PNT","FALSE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&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; row.setValue("LAST_PNT","TRUE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)

del row
del rows
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In my 10.1 scripts, I do use the da.searchcursor, but in this case, I really don't see a noticable improvement in speed.&amp;nbsp; ALSO, there is a bug/known limitation in the da.UpdatCursor and will not work on non SDE registered database.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, this original SearchCursor is much easier to code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As far as speeding things up, comment out/remove your print statements, especially the ones inside a loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;doesn't seem like that would be much of a load, but I have scripts that take orders of magnitude longer to run with my debugging print statements than without them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I comment out all print statements once the script is running right.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Actually, I put something similar to this in my scripts:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;debug = "n"

if debug == "y":print "Processing ",infc

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That way, I just change the value of debug if I want it to print my statements or not.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Of course, even faster if you do the cursor/sort/grab first/last value and assign to variable in one swoop as in my previous post.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:48:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285324#M22028</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-11T13:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285325#M22029</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Karl, I found the bug in your original script:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if now == previous:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("LAST_PNT","FALSE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getvalue("LAST_PNT")
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("LAST_PNT","TRUE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getvalue("LAST_PNT")
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.UpdateRow(row)&amp;nbsp; # note indent fix
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.Next()
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:17:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285325#M22029</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-12T16:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285326#M22030</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I agree with Curt that is why using 0's and 1's is easier if possible for true in false it takes longer to do text and you would not need field calculator the automatic value if 0 when a numeric field is created.&amp;nbsp; But field calculator would be much faster if you need the text values. Taking any of the looping print statements out speeds it up as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;With printing the id out in mine 6,400 records still takes 30s if I remove the print it takes 0.047&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;so big improvement&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Looks like your sort may be slowing it down the most is my guess because your joining two fields so it is processing the field information and sorting at the same time....if you added a field with this definition using "field_calc" for the sort parameters then used that as the sort field it may speed things up as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;AnimalField = "AnimalNum"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SortField = "MSTTime"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#Add Field&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print "Beginning:"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(inPts,"LAST_PNT","TEXT",)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(inPts,"sort_field","TEXT",)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateFieldManagement(sort_field......AnimalField + " A; " + SortField + " D")... "sudo code"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ThesortString = "sort_field"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Jun 2013 19:10:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285326#M22030</guid>
      <dc:creator>LindseyWood</dc:creator>
      <dc:date>2013-06-03T19:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285327#M22031</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thanks,&amp;nbsp;&amp;nbsp; your script didn't quite work for me but it got me to the point where I could get it working...The script below works for me...but there is now a performance issue.&amp;nbsp;&amp;nbsp; This seems to take much longer than it should to process.&amp;nbsp;&amp;nbsp; It probably takes about 3-4 minutes to process only 1000 records.&amp;nbsp;&amp;nbsp; I have similar functions in Excel that can do this in a few seconds.&amp;nbsp; Given that my actual dataset has about 20,000+ records, I don't know how useful this script will really be.&amp;nbsp;&amp;nbsp; Does anybody have any ideas on how to speed this up?&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os
import types
from arcpy import env
#get Parameters from toolbox

#-For Use with Script Toolbox
#inPts = arcpy.getparameterastext(0)
#Animalfield=arcpy.getparameterastext(1)
#SortField=arcpy.getparameterastext(2)

#-For Troubleshooting
inPts = "C:\ArcGIS\Temp\Tester.shp"
AnimalField = "AnimalNum"
SortField = "MSTTime"

#Add Field
print "Beginning:"
arcpy.AddField_management(inPts,"LAST_PNT","TEXT",)

ThesortString = AnimalField + " A; " + SortField + " D"

# Apply Sort
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
rows = arcpy.UpdateCursor(inPts,"","","",ThesortString)
print "Finished Sorting"

thecount = arcpy.GetCount_management(inPts).getOutput(0)
i=0
lastRow = "Starter"
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print i
&amp;nbsp;&amp;nbsp;&amp;nbsp; i=i+1
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print str(count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #thecount = thecount - 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(AnimalField) == lastRow:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("LAST_PNT","FALSE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&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; row.setValue("LAST_PNT","TRUE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)

del row
del rows
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If it is that slow just run Summary Statistics and get the unique result by AnimalNum for the max MSTTime.&amp;nbsp; Should take about a minute to run.&amp;nbsp; You cannot beat it for efficiency.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:48:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285327#M22031</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T13:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285328#M22032</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Actually, I put something similar to this in my scripts:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;debug = "n"
if debug == "y":print "Processing ",infc
&lt;/PRE&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Even more Pythonesque:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;debug = False
...
if debug: print "Processing ", infc
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:48:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285328#M22032</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T13:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285329#M22033</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here is the latest version....It is getting the job done but is still slow.&amp;nbsp;&amp;nbsp; I am intrigued by rzufelt's suggestion of using the searchcursor, but if I understand correctly the searchcursor is read only so I am not sure how I can then work with the LAST_PNT field?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I removed any print functions and it is still not much faster.&amp;nbsp;&amp;nbsp; I will try a combining the 2 sort fields into one to see if that speeds it up. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lastValue = arcpy.SearchCursor(infc, "", "", "", OID + " D").next().getValue(myField) #Get 1st row in cursor - gets last value based on OID

firstValue = arcpy.SearchCursor(infc, "", "", "", OID + " A").next().getValue(myField) #Get last row in cursor - gets first value based on OID
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Working Script&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os
import types
from arcpy import env
#get Parameters from toolbox

#-For Use with Script Toolbox
#inPts = arcpy.getparameterastext(0)
#Animalfield=arcpy.getparameterastext(1)
#SortField=arcpy.getparameterastext(2)

#-For Troubleshooting
inPts = "C:\ArcGIS\Temp\Tester.shp"
AnimalField = "AnimalNum"
SortField = "MSTTime"


#Add Field
print "Beginning:"
arcpy.DeleteField_management(inPts,"LAST_PNT")
arcpy.AddField_management(inPts,"LAST_PNT","SHORT",)
ThesortString = AnimalField + " A; " + SortField + " D"

# Apply Sort
rows = arcpy.UpdateCursor(inPts,"","","",ThesortString)
print "Finished Sorting"

i=0
lastRow = "Starter"

for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print i
&amp;nbsp;&amp;nbsp;&amp;nbsp; #i=i+1
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(AnimalField) == lastRow:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #row.setValue("LAST_PNT",0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&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; row.setValue("LAST_PNT",1)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)

del row
del rows

print "Completed!"
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:48:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285329#M22033</guid>
      <dc:creator>KarlCox</dc:creator>
      <dc:date>2021-12-11T13:48:10Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285330#M22034</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The combined sort field was slightly faster but that is apparently not the holdup.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Jun 2013 19:45:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285330#M22034</guid>
      <dc:creator>KarlCox</dc:creator>
      <dc:date>2013-06-03T19:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285331#M22035</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Here is the latest version....It is getting the job done but is still slow.&amp;nbsp;&amp;nbsp; I am intrigued by rzufelt's suggestion of using the searchcursor, but if I understand correctly the searchcursor is read only so I am not sure how I can then work with the LAST_PNT field?&lt;BR /&gt;&lt;BR /&gt;I removed any print functions and it is still not much faster.&amp;nbsp;&amp;nbsp; I will try a combining the 2 sort fields into one to see if that speeds it up. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;lastValue = arcpy.SearchCursor(infc, "", "", "", OID + " D").next().getValue(myField) #Get 1st row in cursor - gets last value based on OID&amp;nbsp; firstValue = arcpy.SearchCursor(infc, "", "", "", OID + " A").next().getValue(myField) #Get last row in cursor - gets first value based on OID&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Working Script&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy import os import types from arcpy import env #get Parameters from toolbox&amp;nbsp; #-For Use with Script Toolbox #inPts = arcpy.getparameterastext(0) #Animalfield=arcpy.getparameterastext(1) #SortField=arcpy.getparameterastext(2)&amp;nbsp; #-For Troubleshooting inPts = "C:\ArcGIS\Temp\Tester.shp" AnimalField = "AnimalNum" SortField = "MSTTime"&amp;nbsp;&amp;nbsp; #Add Field print "Beginning:" arcpy.DeleteField_management(inPts,"LAST_PNT") arcpy.AddField_management(inPts,"LAST_PNT","SHORT",) ThesortString = AnimalField + " A; " + SortField + " D"&amp;nbsp; # Apply Sort rows = arcpy.UpdateCursor(inPts,"","","",ThesortString) print "Finished Sorting"&amp;nbsp; i=0 lastRow = "Starter"&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp; #print i &amp;nbsp;&amp;nbsp;&amp;nbsp; #i=i+1 &amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(AnimalField) == lastRow: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #row.setValue("LAST_PNT",0) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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; row.setValue("LAST_PNT",1) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastRow=row.getValue(AnimalField) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&amp;nbsp; del row del rows&amp;nbsp; print "Completed!"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If speed is your concern, are your sort fields indexed?&amp;nbsp; Python may not take full advantage of them, but the Summary Statistics with a Join and Select where &amp;lt; MAX_MSTTime then field calculator then invert selection then field calculator would finish this is under 2 minutes for 20,000 records.&amp;nbsp; If you don't want a permanent output do the Summary Statistics to an in memory table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You are reinventing the wheel from my perspective, since the above workflow has been optimized (at least as of 10.1).&amp;nbsp; It will beat the performance of any sorted Python cursor easily, since those cursor are not optimized at all for using indexes in my experience.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Jun 2013 19:47:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285331#M22035</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2013-06-03T19:47:17Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285332#M22036</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;If speed is your concern, are your sort fields indexed?&amp;nbsp; Python may not take full advantage of them, but the Summary Statistics with a Join and Select where &amp;lt; MAX_MSTTime then field calculator then invert selection then field calculator would finish this is under 2 minutes for 20,000 records.&amp;nbsp; If you don't want a permanent output do the Summary Statistics to an in memory table.&lt;BR /&gt;&lt;BR /&gt;You are reinventing the wheel from my perspective, since the above workflow has been optimized (at least as of 10.1).&amp;nbsp; It will beat the performance of any sorted Python cursor easily, since those cursor are not optimized at all for using indexes in my experience.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This actually worked well too...much appreciated.&amp;nbsp;&amp;nbsp; Although I would still like to develop the script to add to a toolbox of custom tools...but unless I can get it working much quicker this technique is the way to go.&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Jun 2013 20:00:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285332#M22036</guid>
      <dc:creator>KarlCox</dc:creator>
      <dc:date>2013-06-03T20:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script to Identify Last of each group</title>
      <link>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285333#M22037</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Using the cursor sort parameter is "slow"... Assuming you have v10.1, this would be the fastest (untested) way I can think of doing it. Otherwise if you have v10.0, you are best off using the SummaryStats tool... Another thing to consider in v10.1 is that the sort option for the .da cursors ONLY works with Geodatabases (not shapefiles, INFO tables, etc.). That said, since this method doesn't rely on the cursor sort option, shapefiles would work fine (actually a bit faster usually than FGDB).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inPts = "C:\ArcGIS\Temp\Tester.shp"
animalField = "AnimalNum"
sortField = "MSTTime"
arcpy.AddField_managment(inPnts, "LAST_FLAG", "SHORT")
animalDateDict = {}
searchRows = arcpy.da.SearchCursor(inPnts, [OID@, animalField, sortField])
for searchRow in searchRows:
&amp;nbsp;&amp;nbsp; oidValue, animalValue, sortValue = searchRow
&amp;nbsp;&amp;nbsp; if animal in animalDateDict:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; animalDateDict[animal].append(sortValue, oidValue)
&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; animalDateDict[animal] = [(sortValue, oidValue)]
del searchRow, searchRows
lastOidsList = [max(animalDict[animal])[-1] for animal in animalDict]
oidFieldName = arcpy.Describe(inPnts).oidFieldName
queryExp = oidFieldName + " in (" + ",".join(str(lastOid) for lastOid in lastOidsList) + ")"
updateRows = arcpy.da.UpdateRows(inPnts, ["LAST_FLAG"], queryExp)
for updateRow in updateRows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; updateRow[0] = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; updateRows.updateRow(updateRow)
del updateRow, updateRows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:48:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-to-identify-last-of-each-group/m-p/285333#M22037</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T13:48:12Z</dc:date>
    </item>
  </channel>
</rss>

