import arcpy from arcpy import env env.workspace = r"C:\Users\cc1\Desktop\NEW.gdb\WAYNE" table = "WAYNE" list = [] with arcpy.da.SearchCursor(table, ["FULL_ADDRESS_NAME"]) as cursor: for row in cursor: list.append(row[0]) del row, cursor with arcpy.da.UpdateCursor(table, ["FULL_ADDRESS_NAME","FEAT_SEQ"]) as updateRows: for updateRow in updateRows: nameValue = updateRow[0] if nameValue in list: updateRow[1] = lutDict[nameValue] updateRows.updateRow(updateRow) del updateRow, updateRows
Solved! Go to Solution.
import arcpy from arcpy import env env.workspace = r"C:\Users\cc1\Desktop\NEW.gdb\WAYNE" table = "WAYNE" uniqueValues = {} values = [] newID = 1 with arcpy.da.UpdateCursor(table, ["FULL_ADDRESS_NAME","FEAT_SEQ"]) as updateRows: for row in updateRows: nameValue = row[0] values.append(nameValue) if nameValue in uniqueVals: row[1] = uniqueValues[[nameValue]] else: newID += 1 uniqueValues[nameValue] = [newID] row[1] = newID updateRows.updateRow(row) del row del updateRows uniqueCount = {} for val in uniqueValues: uniqueCount[val] = values.count(val) with arcpy.da.UpdateCursor(table, ["FULL_ADDRESS_NAME", "FREQ"]) as updateRows: for row in updateRows: nameValue = row[0] count = uniqueCount[nameValue] row[1] = count updateRows.updateRow(row) del row del updateRows
values = [row[0] for row in arcpy.da.SearchCursor(table, ("FULL_ADDRESS_NAME"))]
import arcpy from arcpy import env env.workspace = r"C:\Users\ccooper\Desktop\DATA.gdb\WAYNE" table = "WAYNE" uniqueValues = {} values = [row[0] for row in arcpy.da.SearchCursor(table, ("FULL_ADDRESS_NAME"))] newID = 0 with arcpy.da.UpdateCursor(table, ["FULL_ADDRESS_NAME","FEAT_SEQ"]) as updateRows: for row in updateRows: nameValue = row[0] if nameValue in uniqueValues: row[1] = uniqueValues[nameValue] else: newID += 1 uniqueValues[nameValue] = newID row[1] = newID updateRows.updateRow(row) del row, updateRows uniqueCount = {} for val in uniqueValues: uniqueCount[val] = values.count(val) with arcpy.da.UpdateCursor(table, ["FULL_ADDRESS_NAME", "FREQ_NAME"]) as updateRows: for row in updateRows: nameValue = row[0] row[1] = uniqueCount[nameValue] updateRows.updateRow(row) del row, updateRows
Summary statistics is written in C++. It will be significantly faster than any Python solution.
import arcpy, time myFC = r"C:\my_fgdb.gdb\my_fc" statDict = {} statField = "OID@" caseField = "ELEV" time1 = time.clock() searchRows = arcpy.da.SearchCursor(myFC, [statField,caseField]) for searchRow in searchRows: statValue, caseValue = searchRow if caseValue in statDict: statDict[caseValue].append(statValue) else: statDict[caseValue] = [statValue] sumDict = {} for caseValue in statDict: sumDict[caseValue] = len(statDict[caseValue]), max(statDict[caseValue]) time2 = time.clock()
In addition, the ESRI Summary Statistics tool (and ther Frequency tool) give incorrect results in the output table when the case field values are either NULL or 0. Which was a bug that got fixed a long time ago, but seems to be back (at least in v10.1 SP1).
As a solution to little issues like this, I too have a little collection of Python-based code/tools I have written over the years that are either bug work arounds or major performace enhancments for some of the out of the box geoprocessing tools.
myFC = r"C:\my_fgdb.gdb\my_fc" valueSet = set([r[0] for r in arcpy.da.SearchRows(myFC, ["ORIG_ID"])]) valueList = list(valueSet) valueList.sort() arcpy.AddField_managment(myFC, "SEQ_ID", "LONG") updateRows = arcpy.da.UpdateCursor(myFC, ["ORIG_ID","SEQ_ID"]) for updateRow in updateRows: updateRow[1] = valueList.index(updateRow[0]) + 1 updateRows.updateRow(updateRow) del updateRow, updateRows
import arcpy, collections myFC = r"C:\my_fgdb.gdb\my_fc" valueList = [r[0] for r in arcpy.da.SearchRows(myFC, ["ORIG_ID"])] valueDict = collections.Counter(valueList) uniqueList = valueDict.keys() uniqueList.sort() #if you want SEQ_ID to be sorted numeric or alphabetic arcpy.AddField_managment(myFC, "SEQ_ID", "LONG") arcpy.AddField_managment(myFC, "COUNT", "LONG") updateRows = arcpy.da.UpdateCursor(myFC, ["ORIG_ID","SEQ_ID","COUNT"]) for updateRow in updateRows: updateRow[1] = uniqueList.index(updateRow[0]) + 1 updateRow[2] = valueDict[updateRow[0]] updateRows.updateRow(updateRow) del updateRow, updateRows