I am very new to coding.
I am attempting to use SearchCursor to go through the attribute table of a feature class and return a list of unique years from the years field (which I have done). I am now trying to get values from the mean_total field and get an average for these values for each year in my list, having trouble with using SearchCursor to do this.
import arcpy
# data Q:\ADGIS\GIS329\class08_loopingAndGeoprocessing\gis329_class08_dependencies.gdb\Economic_Workforce_MeanIncome
#obtain input feature class from user
in_featureclass = input('Enter the featureclass path: ')
if arcpy.Exists(in_featureclass):
featureclass_fields = arcpy.ListFields(in_featureclass) # get field objects from the feature class
available_fields = []
for field in featureclass_fields:
#append fields to avaialable_fields list
available_fields.append(field.name)
# Show user what fields are available, ask user to choose fields separated by a space
selected_fields = input('What fields would you like to examine, separated by a space? ' \
'Your options are: {}: '.format(', '.join(available_fields)))
selected_fields = selected_fields.split() #turn user choices into a list of strings
#create an empty list to append all unique values in the year field to
total = 0
count = 0
year_list = []
with arcpy.da.SearchCursor(in_featureclass, selected_fields) as rows:
for row in rows:
if row[0] and row[0] not in year_list:
year_list.append(row[0])
for year in year_list:
for rows[1] in year:
total += total
count += 1
average = total / count
print(year)
print(average)
I'm essentially trying to sort the values in row[1] by year and get the average of that value for each year and result in a print statement for each year that looks like "The average of mean_total for 2010 is $34481.67", repeated for each unique year.
total += total
is probably incorrect -- you'd likely want to change it tototal += row[1]
. – smiller Nov 2 '18 at 18:50