1

I have a feature class which I've been using a relate to compare duplicate field values and was thinking of a way to eliminate the manual process of checking these duplicate values and making a decision as to which records to keep. There are several parameters which determine which record is kept vs. which one is discarded. For now I'd like to flag which records are kept so I can do some spot checking on the attributes.

I have some mock data below to show the desired results in a "Flagged" field. The conditions would involve first using a where clause to prompt the cursor to scan the "ID" field where ID = X. For this selection compare fieldB records, if they are equal values and one of them does not contain the letter 'C' compare fieldA records and flag the one with the higheest numeric value unless there is a null in one of the fieldA records. If one of these records contains a null value, compare the records in fieldC and flag the greatest value.

Feature class records:

fieldA  fieldB  fieldC  Flagged ID
1       ABC     4                X
2       AB      5                X
3       A       6                X
1       EFG     4                Y
2       EF      5                Y
3       E       6                Y

Desired result in "Flagged" field:

fieldA  fieldb  fieldC  Flagged ID
1       ABC     4                X
2       AB      5                X
3       A       6         1X     X
1       EFG     4                Y
2       EF      5                Y
3       E       6         1Y     Y
4
  • What about fieldb values of 'C'? Commented Sep 2, 2015 at 20:55
  • for fieldB where the record contains 'C' within the string I would want to compare the values in fieldA, looking for the greatest numeric value with the exception of the record which contains the letter 'C'. If there are no records in fieldB which contain the letter 'C' I would want to compare all records with the ID 'X'.
    – standard
    Commented Sep 2, 2015 at 21:01
  • 1
    I can't quite figure out all of your conditions, but what it will break down to is a series of if/elif/else statements, as well as making use of python lists and the max () method. Commented Sep 2, 2015 at 21:57
  • Thanks, i figured there would be a series of conditional statements used in the update cursor, what I'm having trouble wrapping my head around is how to group the ID values to start. For example how to tell if ID is X,Y, etc, group them and then run the if/elif/else statements against that group.
    – standard
    Commented Sep 2, 2015 at 22:20

1 Answer 1

3

I think list comprehension as well as applying sql statements to your search cursor will help you out. Here's a few tips:

If you want all the unique values in a field (field ID, in this case).

##Find all unique values in a field
#Return python set of unique values in field "ID"
values = set([r[0] for r in arcpy.da.SearchCursor (table, "ID")])

Max value for field fieldB, limited to ID X:

##Find max value in field 'fieldb' for ID equal to X
#add field delimiters (quote, double-quote, etc)
delimFldB = arcpy.AddFieldDelimiters (table, "fieldB")
#selection sql
sql = "{0} = '{1}'".format (delimFldB, "X")
#max from cursor limited by sql where clause
maxID_X = max ([r[0] for r in arcpy.da.SearchCursor (table, "fieldB", sql)])

Combine them:

##Combine: iterate through each unique value in ID and find max value in field fieldB
#Return python set of unique values in field "ID"
idValues = set([r[0] for r in arcpy.da.SearchCursor (table, "ID")])
#add field delimiters (quote, double-quote, etc)
delimFldB = arcpy.AddFieldDelimiters (table, "fieldB")
#iterate unique ID values
for id in idValues:
    #selection sql
    sql = "{0} = '{1}'".format (delimFldB, id)
    #max from cursor limited by sql where clause
    maxID = max ([r[0] for r in arcpy.da.SearchCursor (table, "fieldB", sql)])
    #print max value by id
    print id, maxID
2
  • ThankS @Emil Brundage, this makes sense, I'll have to look at it a little further next week when I get time but it puts me on the right path I believe.
    – standard
    Commented Sep 3, 2015 at 16:25
  • If you have a more specific problem I'd suggest asking it in a new question. Commented Sep 3, 2015 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.