I am trying to add a new field (float data type) and using existing fields to calculate the new field.
So far, I have:
import arcpy
path = 'pathname' #in my code, this is actually the path name
arcpy.env.workspace = path
fclist = arcpy.ListFeatureClasses()
fc = fclist[1]
prop = 'PrYouth' #name of new field
ftype = "FLOAT" #Type of new field
thisField = arcpy.ListFields(fc, prop)
if len(thisField) == 0: #if length = 0 aka it does not exist yet, add field
arcpy.AddField_management(fc, prop, ftype, "5", "4") # 5 is precision, 4 is scale
else: #delete field then add new again
arcpy.DeleteField_management(fc, prop)
arcpy.AddField_management(fc, prop, ftype, "5", "4") # 5 is precision, 4 is scale
with arcpy.da.UpdateCursor(fc, ['AGE_UNDER5','AGE_5_17','POP2000', prop]) as cursor:
for row in cursor:
kids = row[0] + row [1]
row[3] = (kids/row[2])
cursor.updateRow(row)
I am unable to update the precision and scale values for float. I've tried to specify using field_precision = 5 and field_scale = 4, but this doesn't seem to work.
Also, how do I get the new field to calculate for the proportion? Am I using the right syntax?
Currently, I am only able to add the field and fill the list with 0.
-
Which versions of ArcGIS do you have?Shiuli Pervin– Shiuli Pervin2016年09月29日 09:38:16 +00:00Commented Sep 29, 2016 at 9:38
1 Answer 1
When defining the precision and scale, its expecting an integer, you are currently passing it a string. Change to:
arcpy.AddField_management(fc, prop, ftype, 5, 4)
For the calculation, what are the datatypes of the other fields? Are you performing integer division? If so you need to cast to float first.
Per the doc's, these are ignored if its a personal or file gdb.
http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/add-field.htm
-
I did that at first, but it still didn't work. I tried to do it in model builder, and that was the way the exported python script looked like.Leticia– Leticia2016年09月28日 18:06:51 +00:00Commented Sep 28, 2016 at 18:06
-
Casting as a float first made it work! Thank you! :)Leticia– Leticia2016年09月28日 18:11:31 +00:00Commented Sep 28, 2016 at 18:11
Explore related questions
See similar questions with these tags.