1

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.

Midavalo
30k11 gold badges53 silver badges108 bronze badges
asked Sep 28, 2016 at 17:57
1
  • Which versions of ArcGIS do you have? Commented Sep 29, 2016 at 9:38

1 Answer 1

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

answered Sep 28, 2016 at 18:04
2
  • 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. Commented Sep 28, 2016 at 18:06
  • Casting as a float first made it work! Thank you! :) Commented Sep 28, 2016 at 18:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.