# Identify polygons greater than n-th percentile and perform further analyses using arcpy

My goal is

1. to identify tracts that are above the 99 percentile of a variable (`xvar`),
2. to dissolve contiguous tracts by summing a variable (`yvar`), and
3. to get the coordinates of the centroids (longitude and latitude) of the merged polygons.

At the very first stage for picking the above-99th-percentile tracts, I have got some errors. The following is my current work:

``````import arcpy
import numpy as np

input = r'D:\7362.shp'
myarray = arcpy.da.FeatureClassToNumPyArray(input, ('xvar'))

print myarray
# Printed output:
# [(5.204770523,) (8.6471839055,) (2.1095837756,) ..., (1.4788840302,
# (6.24183499,) (3.6710648163,)]

p99 = np.percentile(myarray, 99)

# use cursor to create the indicator field
# Do I have to create the abovep99 field in the first place???
with arcpy.da.UpdateCursor(input, ['xvar','abovep99']) as cursor:
for row in cursor:
if row[0] > p99:
row[1] = 1
else:
row[1] = 0
cursor.updateRow(row)
``````

The error messages in the percentile calculation `p99 = np.percentile(myarray, 99)` are following:

``````Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
p99 = np.percentile(myarray, 99)
File "C:\Python27\ArcGIS10.3\lib\site-packages\numpy\lib\function_base.py", line 3096, in percentile
return _compute_qth_percentile(sorted, q, axis, out)
File "C:\Python27\ArcGIS10.3\lib\site-packages\numpy\lib\function_base.py", line 3132, in _compute_qth_percentile
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.ndarray'
``````

How do I fix this problem for the percentile calculation?

The numpy array that you are using is composed of uni-dimensional tuple items. Unlike my original answer, introducing axis argument will yield the same error. As suggested by OP, numpy array should be planarised before aplying percentile function, such as `myarray_1 = np.array([arr[0] for arr in myarray])`.
• @faith_dur: Thanks for the links. I converted the tuple items to the list format using the following code: `thelist = [myarray[j][i] for i in range(len(myarray[0])) for j in range(len(myarray))]` – Bill TP Aug 21 '15 at 2:42