How do I insert the string field and calculate it in Python?

Hello I am trying to insert a code that force a column Sum_Acres_ to add a comma to display like this 300,000 from 300000.456

Here is my sample I wrote from a ModelBuilder and export it to python.

    # -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Summary.py
# Created on: 2014-07-16 08:10:58.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

arcpy.ImportToolbox("Model Functions")

# Local variables:
Final_Intersect = "E:\\FY2013_projects\\PMC_project\\USGS EarthExplorer\\2004\\Final_Intersect"
Statistics_Field_s_ = "Acres_1 SUM"
ast_l1b_00301132004175715_20101206173613_5997_ndvi_shp = "E:\\FY2013_projects\\PMC_project\\USGS EarthExplorer\\2004\\Final_Intersect\\ast_l1b_00301132004175715_20101206173613_5997_ndvi.shp"
v_name__dbf = "E:\\FY2013_projects\\PMC_project\\USGS EarthExplorer\\2004\\Final_Summarize\\%name%.dbf"
Name = "ast_l1b_00301132004175715_20101206173613_5997_ndvi"

# Process: Iterate Feature Classes
arcpy.IterateFeatureClasses_mb(Final_Intersect, "A*", "", "RECURSIVE")

# Process: Summary Statistics
arcpy.Statistics_analysis(ast_l1b_00301132004175715_20101206173613_5997_ndvi_shp, v_name__dbf, Statistics_Field_s_, "Crops")

# Process: Parse Path
arcpy.ParsePath_mb(Name, "FILE")


How or where should I insert this code ({:,}'.format(!SUM_Acres_!)) above ?

Actually I have 10 of them in separate modelbuilder and it is the same thing but just a different dictionary and it would save me a lot of my time to do this by copy python and then just simply change the path names to different For example, C:\2004\final , C:\2005\final, C:\2006\final so on...

-
You do realize that IterateFeatureClasses will not work within a python script resources.arcgis.com/en/help/main/10.2/index.html#//… –  Dan Patterson Jul 16 '14 at 19:21
BTW, "{:,}".format(300000.456) != "300,000" –  Paul Jul 16 '14 at 19:23

What's the end goal? If you simply want to display the numbers rounded to whole values with thousands separators, you can do so using Number Formatting within ArcGIS. Search the documentation for "thousands separator" to see all the places this formatting can be configured.

You can use that Python string in a Calculate Field function. Before you can calculate the field, you will need to create a new text field to store the formatted values.

arcpy.AddField_management(v_name__dbf, "formattedsum", "TEXT", "", "", 20)
# Documentation: http://resources.arcgis.com/en/help/main/10.1/index.html#//001700000047000000
arcpy.CalculateField_management(v_name_dbf, "formattedsum", "'{:,}'.format(!SUM_Acres_!)", "PYTHON_9.3")
# Documentation: http://resources.arcgis.com/en/help/main/10.1/index.html#/Calculate_Field/00170000004m000000/ 

-

I would use an UpdateCursor approach. You will first need to add a new text field to your FC so that you can store the commas. You cannot add commas to integer, float, or double type fields:

arcpy.AddField_management(fc, "newField", "TEXT")


Then loop through your rows and apply the proper formatting (e.g. convert 300000.456 to 300,000)

newValue = format(int(row[0]), ",")


Write this newValue variable to the new text field:

row[1] = newValue


Finally, update the row and move on:

cursor.updateRow(row)


Putting it all together:

import arcpy

fc = r'C:\path\to\your.gdb\fc'

with arcpy.da.UpdateCursor(fc, ["number", "newField"]) as cursor:
for row in cursor:
newValue = format(int(row[0]), ",")
row[1] = newValue
cursor.updateRow(row)

-

This is a pretty straightforward task. Hopefully this will get you started:

import os, arcpy

folders_in = [] #Build this manually or with os.walk()/os.listdir()
folders_out = []

exp = '"{:,}".format(!SUM_Acres_!)'

#Loop over each folder and get the shapefiles inside
for f_in, f_out in zip(folders):
arcpy.env.workspace = f_in
shapefiles = arcpy.ListFeatureClasses()

#For each shapefile, calculate the stats and save into the out_folder with base name
for shp in shapefiles:
base,_ = os.path.splitext(shp)
arcpy.Statistics_analysis(shp, os.path.join(f_out, "{}.dbf".format(base),
"Acres_1 SUM", "Crops")
arcpy.CalculateField_management(shp, "name of field here", exp, "PYTHON_9.3")
`
-