[GIS] Calculating Percentiles in ArcMap

arcgis-desktoparcpyfield-calculatornumpystatistics

I have a large polygon dataset (26,000) records with numerous attributes columns. I wish to calculate the percentile rank of each attribute. Say I have population_density in an attribute, is there a simple formula to assign a rank in another attribute field on the percentile score of the value in the first attribute column (pop density)?

Best Answer

You can do this using arcpy in conjunction with numpy:

import arcpy
import numpy as np

input = "c:/data/usa.gdb/USA/counties"
arr = arcpy.da.FeatureClassToNumPyArray(input, ('population_density'))

##to create 3 rank for example
p1 = np.percentile(arr, 33)  # rank = 0
p2 = np.percentile(arr, 67)  # rank = 1
p3 = np.percentile(arr, 100)  # rank = 2

#use cursor to update the new rank field
with arcpy.da.UpdateCursor(input , ['population_density','PerRank']) as cursor:
    for row in cursor:
        if row[0] < p1:
            row[1] = 0  #rank 0
        elif p1 <= row[0] and row[0] < p2:
             row[1] = 1
        else:
             row[1] = 2

        cursor.updateRow(row)

Just modify the code to adapt to your data and change the number of ranks as needed.