0

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.

  • 2
    What specifically are you having trouble with? If you add print statements in your code, are they giving you the expected output (and what line do they stop giving you expected output?). Without a little more information we're kind of debugging blindly. I see that your line total += total is probably incorrect -- you'd likely want to change it to total += row[1]. – smiller Nov 2 '18 at 18:50
  • 1
    It may also be more useful to use a dictionary as your data structure, as it can hold unique years and a list of values. – smiller Nov 2 '18 at 18:52
  • 1
    Welcome to GISSE. Please use the edit button at the bottom of your question to add any clarifications requested in the comments. Add any new information to your question itself, not the comments (makes it easier for people to help you). – jbalk Nov 2 '18 at 19:01
  • 1
    What end result are you trying to achieve? 2 lists? A dictionary? Do you need to update the data you are reading? – jbalk Nov 2 '18 at 19:03
0

The modification below takes advantage of defaultdict, a data structure that allows you to have a default data type of "list" for your dictionary; if the key is not found in the dictionary it doesn't error but instead just creates a list to store your data. Then the average is found by using the built-in functions for lists of sum and length. I'd also recommend adjusting to ensure that "Year" (or the value to average by) is always the first selected field in the cursor, and that the values to average are always the second selected field in the cursor.

import arcpy
from collections import defaultdict

# 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
    # do something to make sure the year is field1, a numeric field is field2...
    year_dict = defaultdict(list)
    with arcpy.da.SearchCursor(in_featureclass, selected_fields) as cursor:
        for row in cursor:
            year_dict[row[0]].append(float(row[1])) # converted to float in case the field contains numbers formatted as string
    for year in year_list.keys():
        total = sum(year_list[year])
        count = len(year_list[year])
        average = total / count
        print("The average for year {} is {}".format(year, average)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.