<?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: Delete Identical but Keep the most recent in Transportation Questions</title>
    <link>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271261#M862</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: mzcoyle&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would use an update cursor, sort by date (newest to oldest) use a list to track all rig_number values, and if the value is already in the list delete that row, if it is not in the list add it and move on.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 19 Sep 2013 15:16:31 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2013-09-19T15:16:31Z</dc:date>
    <item>
      <title>Delete Identical but Keep the most recent</title>
      <link>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271260#M861</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I receive data once a week from a third party vendor. There are 2 fields in which I wish to use to exclude duplicate. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rig_number, and a date field. Currently my script look like this. (standard delete identical code). &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.DeleteIdentical_management(rigsFc,rig_number)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The thing is that I only care about the most recent one and this process seems to select one at random to keep. Is their a way to use delete identical but keep the attribute with the most current date?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Im using python 2.6 and arcMap 10.0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Sep 2013 14:53:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271260#M861</guid>
      <dc:creator>LoganCaraway</dc:creator>
      <dc:date>2013-09-19T14:53:21Z</dc:date>
    </item>
    <item>
      <title>Re: Delete Identical but Keep the most recent</title>
      <link>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271261#M862</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: mzcoyle&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would use an update cursor, sort by date (newest to oldest) use a list to track all rig_number values, and if the value is already in the list delete that row, if it is not in the list add it and move on.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Sep 2013 15:16:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271261#M862</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-09-19T15:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Delete Identical but Keep the most recent</title>
      <link>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271262#M863</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I would use an update cursor, sort by date (newest to oldest) use a list to track all rig_number values, and if the value is already in the list delete that row, if it is not in the list add it and move on.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I gave it a shot. Here is what I have..&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy, os, sys&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;FC = r'M:\GIS\DI_AutomationScript\Test\DI13gdb_237c.gdb\di_Rigs'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Rows = arcpy.SearchCursor(FC,"","","rig_number;date_processed","date_processed D")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;RigList=[]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in Rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue('rig_number') not in RigList:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RigList.append(row.getValue('rig_number'))&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; arcpy.DeleteRows_management(row)&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;BR /&gt;&lt;SPAN&gt;However, an exception was raised...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; exec codeObject in __main__.__dict__&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "\\cw-file1\kgsusers\LCaraway\My Documents\Update cursor.py", line 11, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.DeleteRows_management(row)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 8875, in DeleteRows&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise e&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RuntimeError: Object: Error in executing tool&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any thoughts where I went wrong?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Sep 2013 19:02:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271262#M863</guid>
      <dc:creator>LoganCaraway</dc:creator>
      <dc:date>2013-09-19T19:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Delete Identical but Keep the most recent</title>
      <link>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271263#M864</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: mzcoyle&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You'd want to do something closer to this. Utilizing an update cursor and the deleteRow method of the cursor, not the DeleteRows tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; rig_number = 'RIG_FIELD' date_field_sort = 'DATEFIELD D' update_cursor = arcpy.UpdateCursor(rigsFC, '', '', rig_number, date_field_sort) keep_list = list() for row in update_cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; row_val = row.getValue(rig_number) &amp;nbsp;&amp;nbsp;&amp;nbsp; if row_val not in keep_list: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keep_list.append(row_val) &amp;nbsp;&amp;nbsp;&amp;nbsp; elif row_val in keep_list: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; update_cursor.deleteRow(row) &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Sep 2013 19:07:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271263#M864</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-09-19T19:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: Delete Identical but Keep the most recent</title>
      <link>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271264#M865</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You'd want to do something closer to this. Utilizing an update cursor and the deleteRow method of the cursor, not the DeleteRows tool.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

rig_number = 'RIG_FIELD'
date_field_sort = 'DATEFIELD D'
update_cursor = arcpy.UpdateCursor(rigsFC, '', '', rig_number, date_field_sort)
keep_list = list()
for row in update_cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row_val = row.getValue(rig_number)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row_val not in keep_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keep_list.append(row_val)
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif row_val in keep_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; update_cursor.deleteRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This did the trick! Thanks for your help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am still confused as the difference between a search cursor and an update cursor. Is it that the update cursor is editable and thus methods such as deleteRow can be used with it? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:15:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271264#M865</guid>
      <dc:creator>LoganCaraway</dc:creator>
      <dc:date>2021-12-11T13:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: Delete Identical but Keep the most recent</title>
      <link>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271265#M866</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: mzcoyle&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;This did the trick! Thanks for your help!&lt;BR /&gt;&lt;BR /&gt;I am still confused as the difference between a search cursor and an update cursor. &lt;STRONG&gt;Is it that the update cursor is editable and thus methods such as deleteRow can be used with it? &lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks again!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Exactly. You can read the help docs on cursors here to get a better handle on the difference between cursors.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001q000000"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001q000000&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Sep 2013 20:28:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/delete-identical-but-keep-the-most-recent/m-p/271265#M866</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-09-19T20:28:30Z</dc:date>
    </item>
  </channel>
</rss>

