Select to view content in your preferred language

calculation within an IF statement in arcGIS Pro

2277
12
08-10-2018 08:24 AM
MáireDolan
New Contributor III

I'll begin by stating I am very new to the python world! 

My problem is this....

I am trying to use field calculator in ArcGIS Pro to return a calculation of values in other fields based on an if statement, if that makes sense! Not sure if it can be done?

The Calculation is 

Qrp=Sum(Qs*Ls)/SumLs

Basically thus;

weightAADT(!rp!,!Qs!,!Ls!)

Code Block

def weightAADT(rp, Qs, Ls):
       if rp=="Urban Two Lane":
            return sum(Qs * Ls) / sum(Ls)

my error is "TypeError: 'float' object is not iterable"

Any help appreciated

0 Kudos
12 Replies
FreddieGibson
Regular Contributor II

I would assume that the problem is you're giving the sum built-in function a single number when it expects a collection of numbers. 

2. Built-in Functions — Python 3.7.0 documentation 

sum(iterable[, start])https://docs.python.org/3/library/functions.html#sum

Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable’s items are normally numbers, and the start value is not allowed to be a string.

Are your values Qs and Ls single float values or are they a collection of numbers? 

MáireDolan
New Contributor III

Thanks Freddie, that makes sense.

Qs & Ls values are indeed single float values. What I was trying to achieve was to sum the calculation/values for each row containing the value of the if statement.

0 Kudos
DanPatterson_Retired
MVP Emeritus

The field calculator does one record/row at a time, hence your error as Freddie says.

If you want to vectorize your calculations, you need to use numpy with arcpy's TableToNumPyArray to get the field values into an array format, then arcpy.da.ExtendTable to append the result column onto the original table. Otherwise dump the sum as the error indicates

MáireDolan
New Contributor III

Thanks Dan,

I've never used numpy, could you give me an example of its use?

0 Kudos
DanPatterson_Retired
MVP Emeritus

If each row contains a Qs and an Ls value, then you can calculate

Qs * Ls  as a field calculation …. nothing more.

If the final result is supposed to be the sum of all the (Qs*Ls) values divided by the sum of the Ls values as in

sum(Qs * Ls) / sum(Ls)

then you do that final calculation after the fact by doing a field statistics Sum on that product field and the Ls field.

That equation is a single result which isn't suitable for field calculations

MáireDolan
New Contributor III

Thanks Dan,

I have done the above, but was trying to see if I could do the same in one step without the statistics table.

Trying to figure out the most efficient way of doing same.

If the statistics table is it, then I'll go with that.

0 Kudos
DanPatterson_Retired
MVP Emeritus

How do you want to report it?

Are you using a toolbox and just want one number reported?

Do you actually need the Qs*Ls column data at all?

If not, then if would be possible to just produce a small script to get the values in a searchcursor, do the math and spit out the answer.

or use numpy

input = "c:/data/usa.gdb/some_table"
arr = arcpy.da.TableToNumPyArray(input, ('Qs', 'Ls'))

qs = arr['Qs']

ls = arr['Ls']

# ---- fake data for now
qs
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

ls
array([4., 3., 1., 4., 1., 4., 3., 3., 3., 4.])

#---- now the math in numpy

qs_ls_sum = np.sum(qs*ls)

ls_sum = np.sum(ls)

result = qs_ls_sum/ls_sum

result
4.666666666666667
MáireDolan
New Contributor III

I need the results as field attributes in the original database. The results are needed for further calculations within that database.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I am not sure what the calculation is then.. maybe show a sample couple of rows because there is no way you are going to get that formula in each row.

I suspect that your calculation is (Qs*Ls)/Ls … without the 'sum' thing.

You need to clarify why a sum is needed in any event

0 Kudos