import arcpy, numpy, os ShapeName=arcpy.GetParameterAsText(0) Field=arcpy.GetParameterAsText(1) RankField=arcpy.GetParameterAsText(2) rows = arcpy.SearchCursor(ShapeName) d=[] for row in rows : FieldValue=row.getValue(Field) d.append(FieldValue) d.sort() arcpy.AddMessage(d) del rows rows2 = arcpy.UpdateCursor(ShapeName,"","",RankField,"") row2= rows2.next() while row2: x=0 for i in d: y=dif row2.getValue(Field)==y: rank2=d.index(y) rank=rank2+1 row2.setValue(RankField, rank) rows2.updateRow(row2) x=x+1 row2 = rows2.next() del rows2
def returnvalue(y): if y >= 0 and y <= 3: x = 1 elif y > 3 and y <= 6: x = 2 else: x = 3 return x
print "Starting script" import arcpy arcpy.env.workspace = r"C:\GIS\Default.gdb" Field = "<YOUR FIELD>" currRank = 0 lastRank = 0 lastVal = None rows = arcpy.UpdateCursor("<YOUR FC>", "", "", "<YOUR FIELD>,RANK", "<YOUR FIELD> A") print "Starting loop" for row in rows: currVal = row.getValue(Field) if currVal != lastVal: currRank += 1 row.Rank = currRank elif currVal == lastVal: row.Rank = currRank else: print "Unexpected occurance" lastVal = currVal rows.updateRow(row) del rows print "Done"
Hi!
Thanks for ur reply!
BUT that is not what i am looking for. I speak about several hundred rows. I m looking for some command. Like "rankdata" or sth. like this. I wonder that i didnt find any other entry in the forum. For me it looks odd that this very standard function is not available.
cu
Chris
This worked for me
import arcpy
rank = 1
fc = <the feature class you want to rank>
sqlclause = sql_clause=(None, 'ORDER BY <Ranking Field> DESC')
with arcpy.da.UpdateCursor(fc,'<the rank field>',sql_clause=sqlclause) as cursor:
for row in cursor:
row[1] = rank
cursor.updateRow(row)
rank = rank + 1del cursor
The idea is that I sort the featureclass based on my ranking criteria and then just add 1 to the rank for each loop.
This assumes that you are basing you ranking on a single field.
Instead of DESC which is decending order you could use ASC which is ascending order.
This forum post helped: arcgis 10.1 - Is it possible to sort an arcpy.da.UpdateCursor()? - Geographic Information Systems St...
The code as written represents an ordinal numbering of the table based on a sorting field, it would only represent a ranking if the sorting field contains no duplicate values. Although the OP's example showed uniqueness in the value field, and the provided code snippet works for that example, it is quite common to have duplicate values in a ranking field. Mathew Coyle's code, although using older/original cursors (that is all that was available at the time), does address ranking more generally.
I had missed that, thank you very much for pointing it out.