I think my problem is easy to solve, but so far I couldn't find a solution. I have a Feature Dataset created with the Union tool in ModelBuilder and now try to sum up all values of one specific field (VSG_EINZEL->up to 150 fields) into a new field (VSG_ALL) automatically in ModelBuilder. My problem is, that the number of VSG_EINZEL-fields is rather high and varies so I can’t use a static field calculaion like:
VSG_ALL = VSG_EINZEL + VSG_EINZEL_1 + VSG_EINZEL_12
and so on.
I am very new in using Python.
-
Thanks for your help, but, unfortunately I need do forward the sum of each row to a specific field since this calculation is part of a Model Builder application. Are there any other suggestions? Thank you very much in advance!Michaela– Michaela2013年03月06日 15:53:03 +00:00Commented Mar 6, 2013 at 15:53
3 Answers 3
I would go the route of using cursors. I'm not sure of the field names that you are using, but something like this should work assuming that the VSG_EINZEL fields are numerical:
import arcpy
# use workspace env or give an absolute path for fc
arcpy.env.workspace = "workspace"
fc = "feature_class"
# making a list of fields that contain VSG_EIZEL
# ex VSG_EINZEL, VSG_EIZEL_1, VSG_EIZEL_2
field_prefix = "VSG_EINZEL"
field_list = []
f_list = arcpy.ListFields(fc)
for f in f_list:
if field_prefix in f.name:
field_list.append(f.name)
# use update cursor to find values
# and update the total field
rows = arcpy.UpdateCursor(fc)
for row in rows:
VSG_SUM = 0
for f in field_list:
VSG_SUM += row.getValue(f)
row.VSG_ALL = VSG_SUM
rows.updateRow(row)
del VSG_SUM
del rows
It might need a little adjusting to fit your needs.
-
@myself - I just realized that you, Michaela, were wanting to go through model builder. If you would want to go through python instead, take a look above.gm70560– gm705602013年04月05日 18:25:58 +00:00Commented Apr 5, 2013 at 18:25
I think I did something like this once (not with ModelBuilder), I just added a new field and created a sum of the attributes I was interested in; then I used the summarize function to get a table about my results. In my case, I was after minimum and maximum values as well as the sum so I needed three new fields (and I think I had all kinds of headaches because I didn't even think about the possibility of a tie in the data).
The Summary Statistics tool (in the Analysis Tools | Statistics toolbox) is probably what you're looking for. You can specify that particular field (VSG_EINZEL) and the statistic you want (SUM); it can work with however many values you have in VSG_EINZEL.
The output is a table, which may not be ideal for further calculations -- but hopefully this at least gets you further along. (If you want to do this in Python rather than via Arc tools, the approach would be different, but you could get a numerical value rather than a table.)
For a fully-Python approach, the suggestions here may be useful.
-
Oh, you're summing values from multiple fields, not multiple values in one field -- I misunderstood.Erica– Erica2013年03月06日 16:30:51 +00:00Commented Mar 6, 2013 at 16:30
-
What if you use a Merge operation (Data Management | General) instead of Union? This would bring all your individual polygon features into one multipolygon feature, but instead of unique VSG_EINZEL fields for each one (VSG_EINZEL1, VSH_EINZEL2, etc.), the data would be collated into a single VSG_EINZEL field, making summation easier.Erica– Erica2013年03月06日 16:43:36 +00:00Commented Mar 6, 2013 at 16:43
Explore related questions
See similar questions with these tags.