Python 2.7 -将表字段(Score)中的值添加到基于另一个字段(Category)的最大总数



我在ArcGIS中有表,其中包含有"得分"one_answers"类别"字段的项目行。我试图编写一个函数,从"得分"字段为所有行添加总分,但采用用户提供的变量为每个类别的最大贡献限制。

因此,该函数需要检查每一行所属的"Category",检查"Category"是否已满,如果未满,则将"Score"值添加到Total Score value中。

这部分很简单,但对我来说更困难的部分是类别没有标准化…因此,每次运行该函数时,它都需要读取输入表中"Categories"中所有可能的字段值,读取用户输入的类别限制,并在每行相加时限制每个类别的分数贡献。

这是我到目前为止所做的…

# Set Total Score Variable
totalScore = 0
# Read table "Category" field for possible categories
categories = [row[0] for row in arcpy.da.SearchCursor("Input_Table", "CategoryID")]
uniqueCategories = set(categories)
print(uniqueCategories)
print "Found " + len(uniqueCategories) + "unique categories in table."
# TODO: Read user input to designate category limits somehow...
category_A = 
category_B =
category_C =
category_A_Limit = 
category_B_Limit = 
category_C_Limit = 
with arcpy.da.SearchCursor("Input_Table", ["CategoryID", "Score"]) as cursor:
   for row in cursor:
       if row[0] == category_X:
           if row[0] < category_X_Limit:
               # Read the Score Field for that row
               totalScore += row[1]
       else:
           print "Category "+str(row[0])+" maxed out. This item's score cannot contribute to the total."

一定有更优雅的方法来处理这个…也许是某人知道的特定模块?任何帮助将非常感激!

我已经阅读了arcGIS文档并找到了一些方法:

  1. 执行SQL语句,解释如下:

    http://resources.arcgis.com/en/help/main/10.1/index.html/Executing_SQL_using_an_ArcSDE_connection/002 z00000021000000/

  2. arcpy.da。SearchCursor("Input_Table", "CategoryID"), sql_clause=(None, "GROUP BY Input_Table, CategoryID")].使用实例它应该是和。值。

    http://resources.arcgis.com/en/help/main/10.1/index.html/SearchCursor/018 v00000050000000/http://resources.arcgis.com/en/help/main/10.1/index.html//018 w00000011000000

    这里有解释https://gis.stackexchange.com/a/73300

希望这有帮助。下次,请在这个部分留下你的问题https://gis.stackexchange.com/

最新更新