Select to view content in your preferred language

How do I select the LAST of any number of duplicate records?

2629
13
05-23-2018 03:49 PM
Dean_Heather
New Contributor

I have a table with parcel numbers in one field and soil types in another. There are several soil types for each parcel, and therefore several records for each parcel. 

I am looking for a script (Python, SQL, VB, Arcade) that will iterate through each record grouped by APN, selecting (or outputting) the LAST record for each APN.

Example input table:

APNSOIL TYPE
1A
2A
2A, B
3A
3A, E
3A, E, G

Desired result:

APNSOIL TYPE
1A
2A, B
3A, E, G

It's a simple task that seems like it should be easy, but I can't seem to find a solution. I've been working in Excel, ModelBuilder inside of ArcGIS Pro, and Python in Jupyter Notebook, but I'm not very well versed in any one program or language; maybe there's a better route of attack.

Thank you for any suggestions you might be able to provide!

0 Kudos
13 Replies
JoeBorgione
MVP Emeritus

Okay, cool.  Dictionaries are still a bit of a mystery to me, so I'm glad I'm on the right track: it goes back to what  Joshua suggested yesterday as to what constitutes the desired outcome......

That should just about do it....
0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Joe Borgione , to explain a little more of what is happening with the dictionary in the process, see the snippet below:

table = [[1, "A"], [2, "A"], [2, "A, B"], [3, "A"], [3, "A, E"], [3, "A, E, G"]]

dct = {}
cnt = 0
for row in table:
    cnt += 1
    apn = row[0]
    soil_type = row[1]
    dct[apn] = soil_type
    print("cnt:{}\tapn:{}\tsoil_type:{}\tdct:{}".format(cnt, apn, soil_type, dct))

This will print the dictionary for each row that is being read:

cnt:1     apn:1     soil_type:A          dct:{1: 'A'}
cnt:2     apn:2     soil_type:A          dct:{1: 'A', 2: 'A'}
cnt:3     apn:2     soil_type:A, B          dct:{1: 'A', 2: 'A, B'}
cnt:4     apn:3     soil_type:A          dct:{1: 'A', 2: 'A, B', 3: 'A'}
cnt:5     apn:3     soil_type:A, E          dct:{1: 'A', 2: 'A, B', 3: 'A, E'}
cnt:6     apn:3     soil_type:A, E, G     dct:{1: 'A', 2: 'A, B', 3: 'A, E, G'}
‍‍‍‍‍‍

So, when it finds the same apn key, the value that was stored in the dictionary for that apn key will be replaced by the last value (soil type).

JoeBorgione
MVP Emeritus

Thank you Xander!

Xander Bakker

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

Perhaps way more than needed for the question... but patterns and sequences and their analysis are useful.

Tool url will appear after ArcGIS PRO Beta 2.2 is complete.

Light reading in the interim

Patterns, sequences, occurrence and position