3

I have a shapefile containing counties and their respective population for a given area. I woould like to use the Field Calculator to create a new field that contained the % of the total population in each county. I was looking for a way of doing it in one step inside the Field Calculator, i.e., how to build an expression that states something like "divide the population of each county by the sum of the population of all counties in this same column and multiply it by 100". I'm actually stuck in the "sum of all values" part...

Not sure if I'm demanding too much of a simple solution or if I'm just being blindsided and not seeing some obvious answer. Anyone?

1 Answer 1

3

If you dont have to iterate through to solve multiple fields then you are right on track, but just use statistics to get your total sum (in the attribute table right click on the pop field and choose statistics). Then set up your expression with the exact logic you stated above

[New_Field] =

([POP_COUNTY_FIELD] / Total Population) * 100

Total Population will be the value you found from statistics

3
  • 1
    I was trying to find a more direct solution (one where I could sum the Total Population from within the Field Calculator), but I was also figuring something similar to your solution could work. However,, when I right-click on the field in the attribut table, no statistics option comes up (QGIS 2.10). I worked around it using the Statist plugin. But a more elegant solution would be perfect (like a "Sum" operator in the field calculator).
    – TTome
    Sep 24, 2015 at 19:24
  • Outisde of a python codeblock I am not sure you can do it all in one line, but I could be wrong, I am not at the office and cant check it for ya
    – ed.hank
    Sep 24, 2015 at 19:33
  • Many thanks for your help. I'm starting to believe there is no way of doing it in such a simple way as I wished. Although I will keep on tying to find a way of doing it inside the Field Calculator, I guess I can only get it through a workaround such as geting the total beforehand...
    – TTome
    Sep 24, 2015 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.