2

I am searching for python code to use in Field Calculator to calculate area as a percentage in relation to another field?

enter image description here

The area percentage needs be calculated based on the total area for each "Basin_ID" separately.

6

Here's one way you could do it:

Summary Statistics with the statistics field of sum on Area_Ac and a case field of Basin_ID.

Then join the two tables together, and run Field Calculator like this:

Parser:

Python

Expression:

!Area_Ac!/!<sum field>!

Here is an ArcMap 10.1 script version:

import arcpy

vals = [[r for r in row] for row in arcpy.da.SearchCursor(fc, ["Basin_ID", "Area_Ac"])]
sums = dict([[i, float(sum(zip(*filter(lambda f: f[0] == i, vals))[1]))] 
            for i in sorted(list(set(zip(*vals)[0])))])

with arcpy.da.UpdateCursor(fc, ["Basin_ID", "Area_Ac", "Area_perce"]) as rows:
    for row in rows:

        try:    row[2] = row[1]/sums.get(row[0])
        except:     pass

        rows.updateRow(row)
  • This is probably the easiest way I can think of without scripting. – Fezter Jul 31 '13 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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