https://support.esri.com/en/technical-article/000023355
I want to improve on the above script from ESRI that finds duplicate values and writes the values to a new field in a table. It's using an old cursor and it is very slow with large datasets. But, I'm running into problems due to lack of experience.
Their code verbatim for my purposes:
import arcpy
'''
This script will count the number of occurences of a value in a field ("field_in") and write them to a new field ("field_out")
'''
#path to GDB goes here
arcpy.env.workspace = r"C:\pathto\DuplicateTesting.gdb\DuplicateTesting"
#name of feature class goes here
infeature = "backup_02232021"
field_in = "name"
field_out = "COUNT_"+field_in
arcpy.AddField_management(infeature, field_out,"SHORT")
lista= []
cursor1=arcpy.SearchCursor(infeature)
for row in cursor1:
i=row.getValue(field_in)
lista.append(i)
del cursor1, row
cursor2=arcpy.UpdateCursor(infeature)
for row in cursor2:
i=row.getValue(field_in)
occ=lista.count(i)
row.setValue(field_out, occ)
cursor2.updateRow(row)
del cursor2, row
The following is as far as I got with my improvement. It's throwing an error on the line with the i variable. How can I continue with the UpdateCursor method to put the count of each item in the fields list into its corresponding new field?
infeature = r"C:pathto\DuplicateTesting.gdb\DuplicateTesting
fields = ["name", "location_string_output", "email_address", "cell_phone_number",
"home_phone_number"]
for field_out in fields: #add new fields
arcpy.AddField_management(infeature, "COUNT_"+field_out,"SHORT")
list= []
with arcpy.da.SearchCursor(infeature, fields) as cursor1:
for row in cursor1:
list.append(row)
del cursor1, row
with arcpy.da.UpdateCursor(infeature, fields) as cursor2:
for row in cursor2:
i = row.getValue(row) #<-- throws AttributeError here
occ = list.count(i)
print(occ)
del cursor2, row
Error:
Traceback (most recent call last):
File "\\gisfile\GISstaff\Jared\Python Scripts\ArcGISPro\DuplicateFields_updated.py", line 21, in <module>
i = row.getValue(row)
AttributeError: 'list' object has no attribute 'getValue'