Ranking Fields

2392
7
06-08-2011 07:37 PM
chriss_
New Contributor II
Hi!

What I am looking for is giving the FieldA a Rank depending on the values in FieldB. I would like to end up with sth. like this. Can i do this easy in the field calculator?
Rank     orig. Value
1           5
2           7
3           14
4            23

in python i did it like this:
import arcpy, numpy, os
from arcpy import env


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=d
        if 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



Thanks for any help
Tags (2)
0 Kudos
7 Replies
TerrySilveus
Occasional Contributor III
put this in the code block
 def returnvalue(y):
    if  y >= 0 and y <= 3:
       x = 1
    elif  y > 3 and y <= 6:
       x = 2
    else:
       x = 3
    return x


and in the = box put
returnvalue( !fieldnametocheck! )

substitute your values for ranking and return values as you see fit
0 Kudos
chriss_
New Contributor II
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
0 Kudos
MathewCoyle
Frequent Contributor
I'm not 100% sure what exactly it is you are trying to do, but this will fill in a "Rank" field based on another field of values sorted from lowest to highest.

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"
0 Kudos
TerrySilveus
Occasional Contributor III
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


I see.  Sorry, I misunderstood your question and didn't pay close attention to your code.
0 Kudos
BenjaminSperry1
Occasional Contributor

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 + 1        

del 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... 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
BenjaminSperry1
Occasional Contributor

I had missed that, thank you very much for pointing it out.

0 Kudos