1

I wrote the TOTAL_FREQUENCY values manually. But I want to automatically write this values with a script. Namely, I want to write sum of column 3 to all of the the column 5 rows. How can I do it with a script?

enter image description here

2

The following standalone-script should do the work for ArcGIS 10.x:

import arcpy

arcpy.env.workspace = 'myGeodatabase'

cursor = arcpy.SearchCursor('myLayer', ['frequency'])
sum = 0
for row in cursor:
    sum += row[0]

cursor = arcpy.UpdateCursor('myLayer', ['total_frequency'])
for row in cursor:
    row[0] = sum
    cursor.updateRow(row)

del cursor
  • should I write this script to the field calculator ? – SEDAT AYDEMİR Feb 29 '16 at 11:47
  • No, click the Python-window (should be visible next to the Open ArcToolbox-button in ArcMap). – HimBromBeere Feb 29 '16 at 12:30
  • @HimBromBeere, you are not changing the field between the search and update cursor, thus overwritten the original field values. – artwork21 Feb 29 '16 at 13:00
  • @artwork21 Indeed, you´re right, updated – HimBromBeere Feb 29 '16 at 13:35
  • I dont understand how to do, can you give more details ? how to use this code in ArcMAP ? – SEDAT AYDEMİR Mar 1 '16 at 8:34
1

The following Python script (created by Terry Giles and mentioned in this ESRI forum) allows you to sum the values of a field and paste the sum in another field. The script is designed to work in the Toolbox so that when you run it, you can enter the Layer name, Field name and output:

#args to pass in:
  ##1 = input FC or table
  ##2 = field to sum up
  ##3 = field to write sum into

import arcpy, sys

intable = sys.argv[1]
field = sys.argv[2]

# Create search cursor
rows = arcpy.SearchCursor(intable)
row = rows.Next()
x = 0.0
# Enter while loop for each feature/row
while row:
    x += row.getvalue(field)
    print x
    row = rows.next()
#note value can be rounded depending on field type
gp.calculatefield(intable,sys.argv[3],float(x),"PYTHON")

Disclaimer: I am not an ArcGIS user therefore cannot confirm if this still works as this script was first posted way back in 2007.

  • 1
    arcgisscripting is replaced by arcpy, no need to create a gp-instance. – HimBromBeere Feb 29 '16 at 10:54
  • @HimBromBeere - Many thanks buddy! Edited post but if it is still incorrect, I would rather you post the working script as an answer and I will remove mine =) – Joseph Feb 29 '16 at 10:58
  • 1
    No need to remove it, although it is a bit old it works for the toolbox which is other opportunity. Maybe you should reroll your edit and notice that this works for ArcGIS 9.x. In the current way it won´t work at all I guess. – HimBromBeere Feb 29 '16 at 11:07
0

It seems OP seeking field calculator expression. Here is one:

def getTotal(name,field):
 mxd = arcpy.mapping.MapDocument("CURRENT")
 lr=arcpy.mapping.ListTableViews(mxd,name)[0]
 tbl=[row[0] for row in arcpy.da.TableToNumPyArray(lr,field)]
 return sum(tbl)

=======================================

getTotal( "frekans","frequency")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.