ehm119

Rank values in a field using python

Discussion created by ehm119 on Apr 15, 2011
Latest reply on Apr 15, 2011 by dkwiens
Greetings,

I am trying to take values in one field and write the ranks for those values to a new field.  For example:

3 -> 2
2 -> 1
5 -> 3
6 -> 4
9 -> 6
7 -> 5

In an ideal world, I'd like to deal with ties in a particular way (as follows), but one step at a time...

3 -> 2
2 -> 1
2 -> 1
2 -> 1
5 -> 3
9 -> 5
7 -> 4

Using numpy and scipy, I have gotten this to work using just Python (PythonWin), outside of ArcMap.  The following averages the ranks of ties, which is good enough for now:

import numpy
import scipy
from scipy import stats

x=numpy.ma.array([0,1,1,1,2,2,4,4,5,6])
rank = stats.mstats.rankdata([x])
print rank


Results in:
[[  1    3    3    3    5.5   5.5   7.5   7.5   9   10 ]]

At this point, i am trying to figure out how to pass values from a field into the input array and then write the results back to the other field.  I'm not sure if a cursor is the best way to do this, or if all the values in a field can be passed into an array.  Again, I'm a python newbie, so any ideas or suggestions are welcome.
Thanks,
-Erik

Outcomes