Calculating median in grouped data using ArcMap

6364
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

What's the names of your columns? Are those aliases? I was guessing the spaces were underscores. Replace the names in the 'with arcpy.searchcursor ' line with their proper names

RafaelMichael
Occasional Contributor

0 Kudos
DavidPike
MVP Frequent Contributor

I've edited the code again. Copy and paste that in

RafaelMichael
Occasional Contributor

0 Kudos
DavidPike
MVP Frequent Contributor

Amended the code again

RafaelMichael
Occasional Contributor

work, without error  what now?

0 Kudos
DavidPike
MVP Frequent Contributor

Wow, I'd test the results against your website, bear in mind it may have calculated lower class boundaries different from what you expected. Basically test quite a few features to see if correct result

RafaelMichael
Occasional Contributor

but what should i do now to populate my column 'median' in attribute table? 

0 Kudos
DavidPike
MVP Frequent Contributor

You could pretty easily populate the column by copying and pasting the printed values into your blank column whilst editing the table. Are they correct?

RafaelMichael
Occasional Contributor

I will export the table to excel and paste there the printed IDLE values into the "median" field. The values look fine, thank you for your patience and help

0 Kudos