I'm working with Parcel feature class in ArcGIS Pro 2.2 and trying to figure out the number of parcels that fit into each category. And each category consists of two criteria:
- The development ratio of the total parcel
- The acreage of the parcel
The input data table:
The circled fields are the criteria I am using. And the specific classes are shown in the result spreadsheet screenshot below.
This is the result I generated:
I ran the arcpy script for each individual category:
arcpy.SelectLayerByAttribute_management('ParcelBoundaries_DevTest', 'NEW_SELECTION', '"Developed" > 0.25 and "Developed" <= 0.5 and "GISACRES" <= 1')
arcpy.SelectLayerByAttribute_management('ParcelBoundaries_DevTest', 'NEW_SELECTION', '"Developed" > 0.5 and "Developed" <= 0.75 and "GISACRES" <= 1')
arcpy.SelectLayerByAttribute_management('ParcelBoundaries_DevTest', 'NEW_SELECTION', '"Developed" > 0.75 and "Developed" <= 1 and "GISACRES" <= 1')
After each run, I will see the number of the selected features in the attribute:
Then I will manually input the number into the spread sheet. This is still very tedious. Is there any way that I can use Python or ArcPy to generate the number of the selected features?
-
The GetCount tool should get you the number of selected features.PolyGeo– PolyGeo ♦2018年08月24日 18:38:39 +00:00Commented Aug 24, 2018 at 18:38
-
@BERA I will edit my original post in just a minute. How shall I share the data if possible?Bowen Liu– Bowen Liu2018年08月24日 18:45:44 +00:00Commented Aug 24, 2018 at 18:45
-
@BERA Just did. Please let me know if it makes sense or not.Bowen Liu– Bowen Liu2018年08月24日 18:51:54 +00:00Commented Aug 24, 2018 at 18:51
2 Answers 2
Returns a Result object, whose first output is the count.
Thus to get your count:
count = arcpy.GetCount_management ('ParcelBoundaries_DevTest').getOutput (0)
For whatever reason the return value is a string, so if you want a number just use int
.
count = int (arcpy.GetCount_management ('ParcelBoundaries_DevTest').getOutput (0))
-
Would you care to explain why there is a "(0)" at the end please? Thanks.Bowen Liu– Bowen Liu2018年08月28日 19:34:18 +00:00Commented Aug 28, 2018 at 19:34
-
A Result object has the method
getOutput
. A Result object can have multiple outputs (use the propertyoutputCount
to determine the number).getOutput
requires an index for which output you want. In this case the first output is the count, so we use0
to indicate the first output. See the Result help linked above.Emil Brundage– Emil Brundage2018年08月28日 19:38:53 +00:00Commented Aug 28, 2018 at 19:38
Use pandas module:
pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
import arcpy
import pandas as pd
fc = r'X:\database.gdb\featureclass' #Change to match your input
fields = ['Developed','GISAcres'] #Change to match your input
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,fields),columns=fields)
devbins = [-1,0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] #Add/remove bins
df['Dev_class'] = pd.cut(df[fields[0]], devbins)
acrebins = [-1,1,2,5,10,20,50,100,9999] #Add/remove bins
df['Acre_class'] = pd.cut(df[fields[1]], acrebins)
df2 = pd.pivot_table(data=df, values=fields[0], index='Dev_class', columns='Acre_class',
aggfunc=len)
df2.to_clipboard() #Then paste to excel
Output:
-
1Thank you so much for this detailed response. I just got out of a meeting and won't be able to try it out until next Monday. This seems to be the exact kind of thing I'm looking for. Utmost gratitude and respect for you, who can go this far to help someone else. Really appreciate your time. If you don't mind, I have some general questions about Panda and Python.Bowen Liu– Bowen Liu2018年08月24日 20:56:02 +00:00Commented Aug 24, 2018 at 20:56
-
I am under the impression that you are utterly well-versed in Python and the use of Panda. Right now I am learning Python in my spare time to better automate my GIS and data analysis tasks. I'm wondering shall I stick to getting a good grasp of Python first and get to learn data analysis with Python (numpy, panda etc) later, or shall I pursue both simultaneously? What was your approach? Thanks again!Bowen Liu– Bowen Liu2018年08月24日 20:58:42 +00:00Commented Aug 24, 2018 at 20:58
-
I would not say that :). I know the basics of pandas, enough to know what to google when i dont know. For example by your question i learned pd.cut. Well i read this: esripress.esri.com/display/…, took two ESRI arcpy courses and then learn the rest by trying.Bera– Bera2018年08月25日 06:33:56 +00:00Commented Aug 25, 2018 at 6:33
-
Thanks a lot. I didn't get to log on to see it during the weekend. Just ran it with my own parameters and it worked. This can save me a lot of time so I utterly appreciate your help. I downloaded the book you mentioned. In this case, how did you know that you needed pd.cut to solve this problem? And does 'fc' stand for feature class? I see it almost every arcpy script. Is it already defined? Thanks again!Bowen Liu– Bowen Liu2018年08月27日 20:41:58 +00:00Commented Aug 27, 2018 at 20:41
-
1Thanks a lot for your reply and the link. I think creating two pivot tables is the easier way to go. But I definitely want to learn more pandas function.Bowen Liu– Bowen Liu2018年08月29日 19:23:59 +00:00Commented Aug 29, 2018 at 19:23