Using Get Count to get a percentage and Add Message

1925
9
Jump to solution
02-12-2021 08:17 AM
by Anonymous User
Not applicable

Hello,

I am writing a script from an existing workflow to automate a recurring QC for floodplain data. In the workflow, you need to get the number of passing test points and divide by the total number of points. I am attempting to do this using arcpy.GetCount_management() for the passing points and dividing by the same function for the total number of points. 

I have been successful at getting the function to work and return a print string of the number of results (i.e. "Lyr has 8373 Passing Results"), but I have not been able to perform the division of the two results to get the percentage. Am I missing something? Or is there a better way to go about this? I am relatively new to scripting.

Thanks!

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

.format isn't working since passct is int type.

for it to run get rid of the int type conversion int():

 

passct = (arcpy.GetCount_management(test_pts).getOutput(0))

 

when you want to do any division, ensure both numbers are of the int type - you can test this with print(type(passct))


 

#passct is a string
passct_string = (arcpy.GetCount_management(test_pts).getOutput(0))
#passct is an int
passct_int = int(arcpy.GetCount_management(test_pts).getOutput(0))

 

View solution in original post

0 Kudos
9 Replies
by Anonymous User
Not applicable

It would help seeing the code to see what your expression is but for the sake of throwing darts, are you casting the values to int's before dividing?

0 Kudos
by Anonymous User
Not applicable

Here is a sample of the code (using ArcPro) for selecting passing points and getting the count. I have tried to cast the output as an integer but I get this error:

arcpy.management.SelectLayerByAttribute(test_pts, 'NEW_SELECTION',"Status = 'Pass'", 'NON_INVERT')
passct = int(arcpy.GetCount_management(test_pts).getOutput(0))
print('{} has {} Passing records'.format(test_pts, passct[0]))

Traceback (most recent call last):
File "<string>", line 2, in <module>
TypeError: 'int' object is not subscriptable

0 Kudos
by Anonymous User
Not applicable

in your print statement you are trying to index the int passct[0].  Although, you might not be able to cast like that if I remember correctly.

Try it like this:

result = arcpy.GetCount_management(fc)
val = int(result.getOutput(0))

 

0 Kudos
DavidPike
MVP Frequent Contributor

Can you share the script? Do you have an error message?

 

0 Kudos
by Anonymous User
Not applicable

Here is a sample of the code (using ArcPro) for selecting passing points and getting the count. I have tried to cast the output as an integer but I get this error:

arcpy.management.SelectLayerByAttribute(test_pts, 'NEW_SELECTION',"Status = 'Pass'", 'NON_INVERT')
passct = int(arcpy.GetCount_management(test_pts).getOutput(0))
print('{} has {} Passing records'.format(test_pts, passct[0]))

Traceback (most recent call last):
File "<string>", line 2, in <module>
TypeError: 'int' object is not subscriptable

0 Kudos
DavidPike
MVP Frequent Contributor

.format isn't working since passct is int type.

for it to run get rid of the int type conversion int():

 

passct = (arcpy.GetCount_management(test_pts).getOutput(0))

 

when you want to do any division, ensure both numbers are of the int type - you can test this with print(type(passct))


 

#passct is a string
passct_string = (arcpy.GetCount_management(test_pts).getOutput(0))
#passct is an int
passct_int = int(arcpy.GetCount_management(test_pts).getOutput(0))

 

0 Kudos
by Anonymous User
Not applicable

Thanks for the clarification. I was using the print and .format to view the result but did not know that the integer would not allow this. I was able to do the division after converting the string to int. Thanks!

0 Kudos
DavidPike
MVP Frequent Contributor

Also just note that integer division won't always work as you expect depending on your Python version.  I think you're using ArcGIS Pro so you should be fine. e.g. 1/2 = 0.5 in the Pro Python window, but returns 0 in Python 2.7.

DanPatterson
MVP Esteemed Contributor

Then of course one could read the help and see examples for "immediate" mode and in code.  This way you can ensure that your inputs are correct and check to see the data type that it returns to see whether casting is needed to get a numeric type

Get Count (Data Management)—ArcGIS Pro | Documentation

 


... sort of retired...
0 Kudos