I have a dataset with 2 attributes: area & type. Area is different for each polygon. There are only several types, about 20. For each type I need the polygons prioritized by area. So the largest area for type X gets priority 1, second-largest gets 2 etc. Then repeat this for type Q and all the other types.
This needs to be done in the field-calculator, not by a model or separate python script.
I have found a way to do incremental values for the whole dataset, or for a selection in that dataset. This can be found here: How To: Create sequential numbers in a field using Python in the Field Calculator
Drawback is that I need to select each type by hand, and execute the 'incremental values' field calculation. Is there a way to do it in one go, automatically?
Solved! Go to Solution.
The Rank features by Field A for each unique value in Field B question is more relevant to your situation.
Try using the following code after you change the layer and field names that apply to your situation:
import arcpy
from itertools import count, groupby
layer = "sample"
fields = ["first_sort_field","second_sort_field", "rank_field"]
sql = "ORDER BY first_sort_field, second_sort_field DESC"
with arcpy.da.UpdateCursor(layer,fields, sql_clause=(None,sql)) as cur:
for k,g in groupby(cur, key=lambda x: x[0]):
ctr = count(1)
for row in g:
cur.updateRow(row[:-1] + [next(ctr)])
This needs to be done in the field-calculator, not by a model or separate python script.
Why? Doing what you want with a cursor is quite straightforward and can be pasted in the interactive Python window.
ok, I see. Yes, it can also be done in the Python window. Still, I don't know how to and don't have time or money to do a python course...
should I have posted this somewhere else? sorry, new to this forum.
I really 'can't see the trees through the forest', there are so many options.
I found this: Is it possible to use a Group By function in the arcpy Update cursor? and this https://gis.stackexchange.com/questions/200150/auto-incrementing-field-based-on-groups-within-featur... , all seem tantalizing little bits which are just beyond my understanding, I can't integrate it all into one working bit of script.
The Rank features by Field A for each unique value in Field B question is more relevant to your situation.
Try using the following code after you change the layer and field names that apply to your situation:
import arcpy
from itertools import count, groupby
layer = "sample"
fields = ["first_sort_field","second_sort_field", "rank_field"]
sql = "ORDER BY first_sort_field, second_sort_field DESC"
with arcpy.da.UpdateCursor(layer,fields, sql_clause=(None,sql)) as cur:
for k,g in groupby(cur, key=lambda x: x[0]):
ctr = count(1)
for row in g:
cur.updateRow(row[:-1] + [next(ctr)])
Joshua, THANK YOU! This worked at the first try. I do know my SQL, but Python code is not my strength. At all... So thank you very much! We will be able to adapt this for many other uses. Greetings from the Netherlands, by the way. From my attic in semi-lockdown.