Select to view content in your preferred language

Calculating median in grouped data using ArcMap

6883
53
Jump to solution
01-31-2020 10:46 AM
RafaelMichael
Occasional Contributor

I got stuck when calculating the median age in grouped data (age ranges) for each feature (each feature is a single administrative unit). I have about 18,000 features.

Field names indicate age ranges: 0-14, 15-64, 65-100. In the "median" field I would like to calculate the median age for each feature. I found a website https://atozmath.com/StatsG.aspx?q=1 where you can calculate such a median for a single feature, for example the first feature from the attribute table above.

The page easily calculated the median for the first feature in the image, and the page also wrote how it calculated it, but I don't know how to write a universal script for a "median" field to calculate the median for each feature.

0 Kudos
53 Replies
DavidPike
MVP Frequent Contributor

edited

RafaelMichael
Occasional Contributor



same error

0 Kudos
DavidPike
MVP Frequent Contributor

edited

RafaelMichael
Occasional Contributor

just printed "complete" without printed values

0 Kudos
DavidPike
MVP Frequent Contributor

Check your attribute table, should have populated it

RafaelMichael
Occasional Contributor

the column has filled up but a huge number of invalid values in the median column

0 Kudos
DavidPike
MVP Frequent Contributor

Wow looks awful.

I've edited the code to just spit out the values, -999 represents rows where no median could be computed.

RafaelMichael
Occasional Contributor

again, it only prints 10,000 values instead of 18,000, without 0 and null

0 Kudos
DavidPike
MVP Frequent Contributor

Edited. If that doesnt work  try it on a subset of features, say 100

RafaelMichael
Occasional Contributor

prints all values for which the median can be calculated and those for which cannot be calculated (as -9999). I exported the attribute table to Excel and pasted the values printed in Python IDLE and everything is fine now!  Can you do it so that the values are directly saved in the ArcGIS attribute table?

0 Kudos