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:
1 Answer 1
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()
-
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.Paul– Paul2018年01月20日 20:28:55 +00:00Commented Jan 20, 2018 at 20:28 -
collections.defaultdict