Attribute table statistics using arcpy (for polyline)

897
4
Jump to solution
03-30-2021 03:22 AM
MohamadAchour
New Contributor III

hello, I have a polyline shapefile, and I need to calculate the mean value of one column of its attribute table using arcpy.

Any suggestion?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
import numpy as np
from arcpy.da import SearchCursor
values = np.mean([row[0] for row in SearchCursor(fc, "Shape_Length")])

An equivalent but slower method, if there are no nulls (an extra hoop to jump through is necessary if there are nulls)


... sort of retired...

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor

As in Summary Statistics (Analysis)—ArcGIS Pro | Documentation

or is it something different you are looking for?


... sort of retired...
0 Kudos
DanPatterson
MVP Esteemed Contributor

or perhaps

import numpy as np
from arcpy.da import TableToNumPyArray
fc = r"C:\Git_Dan\npgeom_bak\Project_npg\npgeom.gdb\Ontario_singlepart"
arr = TableToNumPyArray(fc, "Shape_Length", skip_nulls=True)

np.mean(arr["Shape_Length"])
84684.37997765756

... sort of retired...
MohamadAchour
New Contributor III

I will try this one. 

Thanks a lot.

0 Kudos
DanPatterson
MVP Esteemed Contributor
import numpy as np
from arcpy.da import SearchCursor
values = np.mean([row[0] for row in SearchCursor(fc, "Shape_Length")])

An equivalent but slower method, if there are no nulls (an extra hoop to jump through is necessary if there are nulls)


... sort of retired...