I am attempting to order based on two fields within an arcpy cursor so that I can then eventually delete the rows I do not want. Below is an example of the table I am working with:
Within each grouping of "El_Season" that is the same (in this case OIDs 1&2 and OIDs 3&4) I would like to sort based on "COUNT_OccProb" and by "prob_code" in descending order within a cursor so that once sorted, I can delete the extras within groups that have lower values. Currently, the code does not sort successfully and is just printing OIDs 2 and 4 as the extras where it should be printing OIDs 1 and 3 if working as I would like. This is the code that I am working with:
case_fields = ["El_Season"] max_field = ["COUNT_OccProb", "prob_code"] sql_orderby = "ORDER BY {}, {} DESC".format(case_fields, ",".join(max_field)) with arcpy.da.UpdateCursor(statistics, "*", sql_clause=(None, sql_orderby)) as cursor: case_func = itemgetter(*(cursor.fields.index(fld) for fld in case_fields)) for key, group in groupby(cursor, case_func): next(group) for extra in group: print extra # currently have it printing extra instead of deleting for testing purposes
Solved! Go to Solution.
If you can mark one of the comments as the answer, it will let others know you aren't seeking additional feedback.