I am searching for python code to use in Field Calculator to calculate area as a percentage in relation to another field?
The area percentage needs be calculated based on the total area for each "Basin_ID" separately.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
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:
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 == i, vals))))] for i in sorted(list(set(zip(*vals))))]) with arcpy.da.UpdateCursor(fc, ["Basin_ID", "Area_Ac", "Area_perce"]) as rows: for row in rows: try: row = row/sums.get(row) except: pass rows.updateRow(row)