3

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:

INPUT

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:

Selection Result

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:

Selected

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?

asked Aug 24, 2018 at 18:27
3
  • The GetCount tool should get you the number of selected features. Commented 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? Commented Aug 24, 2018 at 18:45
  • @BERA Just did. Please let me know if it makes sense or not. Commented Aug 24, 2018 at 18:51

2 Answers 2

9

Get Count.

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))
answered Aug 24, 2018 at 19:08
2
  • Would you care to explain why there is a "(0)" at the end please? Thanks. Commented Aug 28, 2018 at 19:34
  • A Result object has the method getOutput. A Result object can have multiple outputs (use the property outputCount to determine the number). getOutput requires an index for which output you want. In this case the first output is the count, so we use 0 to indicate the first output. See the Result help linked above. Commented Aug 28, 2018 at 19:38
1

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:

enter image description here

answered Aug 24, 2018 at 19:28
9
  • 1
    Thank 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. Commented 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! Commented 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. Commented 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! Commented Aug 27, 2018 at 20:41
  • 1
    Thanks 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. Commented Aug 29, 2018 at 19:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.