I'm trying to calculate multiple fields that all begin with "B01". This calculation consists of multiplying the fields by a field that contains a percentage ("TractPercent") and then dividing the results of that multiplication by a population field ("BufferPOP").
The problem I'm having is incorporating the variable associated with my "for loop" with my Python3 expression.
Here is my code:
import arcpy
### set workspaces
arcpy.env.workspace = r"C:\arcGIS_Shared\Python\Demographics.gdb"
relpath = r'C:\arcGIS_Shared\Python'
p = arcpy.mp.ArcGISProject(relpath + r'\Demographics.aprx')
m = p.listMaps('Map')[0]
layers = m.listLayers("DalyCity_HalfMile_Demographics")
for layer in layers:
print(layer)
DemoFields = arcpy.ListFields(layer, "B01*", "ALL")
for DemoField in DemoFields:
arcpy.CalculateField_management(layer, DemoField, "(+DemoField+ *
!TractPercent!) / !BufferPOP!", "PYTHON3")
print(layer.name + ' ' + DemoField + ' Calculated')
When I run the code this way I get the following error, but can't figure out what I need to change.
ERROR 000622: Failed to execute (Calculate Field). Parameters are not valid.
ERROR 000623: Invalid value type for parameter field
-
1What's the format of the DemoField? What happens if you add some print statements - are you getting the values you expect?SMiller– SMiller2018年05月07日 15:20:54 +00:00Commented May 7, 2018 at 15:20
-
@smiller I added some print statements and they returned what I expected them to. As far as the format, the data type is "Double". Is that what you were asking for?Matt A.– Matt A.2018年05月07日 15:41:21 +00:00Commented May 7, 2018 at 15:41
-
@MattA., could you give us an example of what the print statement returned? And I think smiller meant to print the expression itself. Did you try testing the result directly in the Field Calculator, not using ArcPy?Tom– Tom2018年05月07日 16:20:02 +00:00Commented May 7, 2018 at 16:20
1 Answer 1
Your expression shouldn't include variable names. You're writing:
"(+DemoField+ * !TractPercent!) / !BufferPOP!"
but it should be:
"(!{}! * !TractPercent!) / !BufferPOP!".format(DemoField.name)
-
1Shouldn't it be
DemoField.name
?Bjorn– Bjorn2018年05月07日 15:44:33 +00:00Commented May 7, 2018 at 15:44 -
I've tried both
"(!{}! * !TractPercent!) / !BufferPOP!".format(DemoField)
and"(!{}! * !TractPercent!) / !BufferPOP!".format(DemoField.name)
, but I am still getting the same errors. I looked at the data types associated with theCalculateField_management
, (pro.arcgis.com/en/pro-app/tool-reference/data-management/…) but didn't see anything that stood out. Except for maybe that.format(DemoField)
is outside the quotesMatt A.– Matt A.2018年05月07日 16:14:42 +00:00Commented May 7, 2018 at 16:14 -
Thanks, Bjorn. I always get a list of names with list comprehension, so I overlooked that. It's fixed. @MattA., it's supposed to be outside the quotes. Otherwise, you're simply looking for a field that matches your variable name rather than the value held by the variable.Tom– Tom2018年05月07日 16:18:05 +00:00Commented May 7, 2018 at 16:18
-
1@MattA., what are the datatypes of each of your fields?Tom– Tom2018年05月07日 16:27:09 +00:00Commented May 7, 2018 at 16:27
-
@Tom I tried running the code in the Code Block of the Field Calculator and got a similar error about a missing or incorrect parameter. The datatypes for all the fields I'm using in the code is "Double". As far as the print statements, I was able to successfully print the the Table name and the DemoField name. I'm not sure how I would print the expression, unless I assigned it to a variable.Matt A.– Matt A.2018年05月07日 17:32:30 +00:00Commented May 7, 2018 at 17:32
Explore related questions
See similar questions with these tags.