Create Unique ID

4690
7
12-27-2015 01:19 PM
IanIrmischer
New Contributor III

I have a large featureclass with over 100000 records.  I have a field called Last_name.  There are 200 unique last names in this field such as "jones" or "smith".  I would like to create a new field called UID that puts a number in this field based on the last name.  Since I have 200 last names, this field would then have just 200 numbers in it from 1 to 200.  This will be used to remove personal information from the featureclass.  Is there a function in arcpy to create a new field with unique IDs based on another field?

Or is there a script I can use in the field calculator to accomplish this?

Thanks,

Ian

0 Kudos
7 Replies
RebeccaStrauch__GISP
MVP Emeritus

One way you could do this is to create another table with the unique last names and a unique ID field. There are several methods for doing that including running statistics agains your attribute table, or using the Frequency—Help | ArcGIS for Desktop  command. 

Then you can relate the two tables with the name field, and use the unique ID in the new table to calculate the unique ID in the attribute table.  This should be a fairly fast process with only a few steps, so a script is probably not needed.

IanIrmischer
New Contributor III

Thanks. I thought there might be something canned for that purpose since

most stats programs have a function for that. I guess it is not as common

as I thought. Great suggestion though.

Ian

On Sun, Dec 27, 2015 at 1:37 PM, Rebecca Strauch, GISP <geonet@esri.com>

0 Kudos
JamesCrandall
MVP Frequent Contributor

If you want a python solution using arcpy, this example shows converting the FeatureClass to a NumPy array, get the unique values of the desired field then running an da.UpdateCursor with an expression to set the associated integer value field.  You'd probably be able to just do this much faster manually as Rebecca suggested, but this could be part of a python implementation I guess.

I tested this on sample data and it populates the "uvalID" short integer field (that I added to the feature class) with a value incrementing from 1 using the unique values in the field "MCITY".

def AddIDToUniqueValues():
    datarr = []
    fc = r'H:\Documents\ArcGIS\Default.gdb\L_Join_Output'
    numparr = arcpy.da.FeatureClassToNumPyArray(fc, ['OBJECTID','MCITY'])
    uniquevals = np.unique(numparr['MCITY'])
    assocval = 1
    updatefield = 'uvalID'
    for uval in uniquevals:
        print uval
        expr = "MCITY= '"+ uval + "'"
        with arcpy.da.UpdateCursor(fc, updatefield, expr) as ucur:
            print expr
            for row in ucur:
                print assocval
                row[0] = assocval
                ucur.updateRow(row)
                
        assocval = assocval + 1
ChrisSnyder
Regular Contributor III

Another method using Python:

myTbl = r"C:\temp\my_fgdb.gdb\my_table"
arcpy.AddField_management(myTbl, "UNIQ_ID", "LONG")
dataDict = {}
i = 1
updateRows = arcpy.da.UpdateCursor(myTbl, ["LAST_NAME", "UNIQ_ID"])
for updateRow in updateRows:
    if updateRow[0] not in dataDict:
        dataDict[updateRow[0]] = i
        i+=1
    updateRow[1] = dataDict[updateRow[0]]
    updateRows.updateRow(updateRow)
del updateRow, updateRows
JamesCrandall
MVP Frequent Contributor

Chris' approach has better performance!

0 Kudos
DanPatterson_Retired
MVP Emeritus

A more focused python/numpy solution is published here, if you don't want to use cursors... Before I forget ... # 19 ... Reclassification of arrays ...

JacobTodd1
New Contributor III

Hi Ian,

You could also give this script a try as well.

import arcpy

ws = arcpy.env.workspace = r"<workspace path>"

fc= "<feature class>"

field_LN = "<Last Name field>"

field_UID = "UID"

#Create field list

fields = arcpy.ListFields(fc, "", "")

#Create an empty set to be populated with unique last names

seen = set()

#Loop through fields and perform a search cursor to find the unique last names and then

for field in fields:

    with arcpy.da.SearchCursor(fc, field_LN) as sCur:

        for row in sCur:

            items = row[0]

            #Add unique last names to the set()

            seen.add(items)

#Convert the set to a list

uniqueValues = list(seen)

#Loop through unique value list

for item in uniqueValues:

    #Perform an update cursor that updates a unique value field to that of the last name's index in the unique values list

    with arcpy.da.UpdateCursor(fc, [field_LN, field_UID]) as iCur:

        for row in iCur:

            if row[0] == item:

                row[1] = uniqueValues.index(item)

            if row[0] == item:

                row[1] = uniqueValues.index(item)

            if row[0] == item:

                row[1] = uniqueValues.index(item)

            iCur.updateRow(row)

Hopefully this will help you achieve your desired result.

- Jacob

0 Kudos