Select to view content in your preferred language

Sum rows of a field

1103
3
11-23-2011 07:51 AM
RichardThurau
Deactivated User
Hi,
I'm trying to create a single scalar value as the sum of a row, then use the scalar in a field calculation:

I want the sum of "UTC10_FT" call it tot_utc_ft, then use that value in a calculate field:

to find the sum value:

list2 = []
rows = arcpy.SearchCursor(fc)
for row in rows:
    utc_ft = row.getValue("UTC10_Ft")
    list2.append(utc_ft)
    tot_utc_ft = sum(list2)

arcpy.CalculateField_management(parcels, "Tot_UTC_Pct", """!UTC10_Ft! / tot_utc_ft""", "PYTHON")

Currently getting this error:
    tot_utc_ft = sum(list)
TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'

I'm sure this is something simple, but admittedly, not a great python programmer.

The next question will be whether python will use my tot_utc_ft variable within field calculator? Or, do I need to perform this operation within an expression?

Thanks for your help.

rich
Tags (2)
0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Rich,

I believe you are receiving that error because you have a NULL, or empty, value for one of the rows.  Perform a search by attribute and then convert all NULL values to 0 and try your script again.  Also, you will be able to use the variable in the field calculator.
0 Kudos
HemingZhu
Frequent Contributor
Hi,
I'm trying to create a single scalar value as the sum of a row, then use the scalar in a field calculation:

I want the sum of "UTC10_FT" call it tot_utc_ft, then use that value in a calculate field:

to find the sum value:

list2 = []
rows = arcpy.SearchCursor(fc)
for row in rows:
    utc_ft = row.getValue("UTC10_Ft")
    list2.append(utc_ft)
    tot_utc_ft = sum(list2)

arcpy.CalculateField_management(parcels, "Tot_UTC_Pct", """!UTC10_Ft! / tot_utc_ft""", "PYTHON")

Currently getting this error:
    tot_utc_ft = sum(list)
TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'

I'm sure this is something simple, but admittedly, not a great python programmer.

The next question will be whether python will use my tot_utc_ft variable within field calculator? Or, do I need to perform this operation within an expression?

Thanks for your help.

rich


Looks like some of your row.UTC10_Ft has null value in it. -never mind. Jake says it
0 Kudos
RichardThurau
Deactivated User
Many Thanks!! Pesky nulls.
0 Kudos