How do I add multiple fields to a table in Model Builder ArcGIS 10.4?

3011
4
09-06-2016 09:05 AM
JustinGleeson2
New Contributor

I am developing a new model for a population profile geoprocessing task. The first step sums a series of variables based on a simple point in polygon routine. I then want to add a new set of field names to the table to create some percentages using Calculate Field. 

This is possible using 'Add Field' but I need to use the tool several times and it is cumbersome as I have a high number of fields to add in.

Is there a way to use an iterator to run Add Field?

much appreciated

Justin

0 Kudos
4 Replies
DarrenWiens2
MVP Honored Contributor

You could use a For iterator (e.g. "For 1 to 50" to do something, like Add Field, 50 times [or possible 49 times, depending how it counts]). This would add 50 copies of very similar fields, like all fields of the same type, with incrementing numbers in their names, like "My_Field_1", "My_Field_2", etc.

Or, you could populate a table with your Add Field parameters (columns holding info like field name, field type, etc.) and run the Iterate Row Selection to get those values to use in Add Field, so you could have full control over the types and names of all your new fields.

JustinGleeson2
New Contributor

Thanks Darren, the Iterate Row Selection seems to be the way to go. I have set this up and it takes it's values from a table with the required parameters in Add Field. I still seem to be running into trouble with it though. 

Would you have the Iterate Row Selection and Add Field working off a secondary Model within the main Model?

Thanks

Justin

0 Kudos
BruceHarold
Esri Regular Contributor

A good pattern is to make a numpy array of typed data and use arcpy.da.ExtentTable to add the fields to your table.

The Python snippet would be in a Calculate Value model tool.  An example of syntax is in this GP sample:

http://www.arcgis.com/home/item.html?id=da9efbe1232f426ead182213f53c8cd8 

JustinGleeson1
New Contributor

After some different approaches to this I created a simple .py script that updates the table with all the fields I need. See below. 

...............................

import arcpy
from arcpy import env
env.workspace = r"C:\X\X.gdb"

for PopSummary in arcpy.ListTables("*"):
try:
arcpy.AlterField_management(PopSummary, "Sum_Pop_T11", "", "TotalPop")
arcpy.AlterField_management(PopSummary, "Sum_Pop_014T11", "", "Pop_014")
arcpy.AlterField_management(PopSummary, "Sum_Pop_1524T1", "", "Pop_1524")
except:
pass

..............................

Lot's of other useful way of updating a table using the Fields Toolset within the script - Alter Field, Calculate Field etc

0 Kudos