1

I am trying to modify an existing table that I can relate to a feature in Arcmap(v10.5).

Currently, the table fields and values look like this: Sample data table

I would like to summarize the table. For each unique "property number" value, I would like to have the table list unique "fagtype" values. And for each "fagtype value", I would like the table to sum the "fagacres" for each value. I would like the "facres" and "fagirrig" fields to just list unique values as well.

I found a starting place under the "search cursor" summary on the ESRI help page. But it's a very limited start and I'm not sure where to go from here, or even if a searchcursor is the best route.

import arcpy
agtable = arcpy.GetParameterAsText(0)
fields = ['PID', 'fagtype', 'fagacres', 'facres', 'fagirrig']
with arcpy.da.SearchCursor(agtable, fields) as cursor:
 for row in cursor:
asked Jan 19, 2018 at 17:16
4
  • 2
    I'd take a look at Summary Stats. If you want to use search cursors, take a look at collections.defaultdict Commented Jan 19, 2018 at 17:34
  • Looks like the default dictionaries are the way to go. Commented Jan 19, 2018 at 17:45
  • Did you try using SQL? It looks to me like a group by and a sum would do wonders. Commented Jan 19, 2018 at 17:52
  • I'm very limited in SQL knowledge. However, I do know what your talking about. And that probably is the best way. Commented Jan 19, 2018 at 18:14

1 Answer 1

4

Sounds like a simple job for Summary Statistics. But if you want to use python, pandas is a great module for tasks like this and is included in ArcGIS 10.5:

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.

You can create a pandas dataframe from a feature class table using the da.SearchCursor and then groupby and sum:

import arcpy
import pandas as pd
feature_class = r'C:\database.gdb\feature_class' #Change
fc_fields = ['Property_number','fagtype','fagacres','facres'] #Change
#Function to read a feature class and return a pandas dataframe
def fc_to_dataframe(fc, fields):
 cursor = (i for i in arcpy.da.SearchCursor(fc, fields))
 df = pd.DataFrame(data=cursor, columns=fields)
 return df
#Call function
df = fc_to_dataframe(feature_class, fc_fields)
#Group and sum
print df.groupby(['Property_number','fagtype']).sum()
answered Jan 19, 2018 at 18:10
2
  • 1
    +1 Pandas is amazing. You can read directly from the cursor if you use pd.DataFrame.from_records(arcpy.da.SearchCursor(fc, fields), columns=fields) No need to wrap in another generator. Commented Jan 20, 2018 at 20:28
  • Nice did not know that Commented Jan 21, 2018 at 17:17

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.