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!
Solved! Go to Solution.
.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))
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?
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
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))
Can you share the script? Do you have an error message?
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
.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))
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!
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.
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