1

I am trying to calculate the sum in each field in my list:

Cost_Adders = ["RR_CROSSING", "INTERSTATE", "RIVER"]
for Cost in Cost_Adders:
    where = f"Cost_Adders = {Cost}"
    CostAdder_count = sum([row[0] for row in arcpy.da.SearchCursor(Layer, Cost_Adders, where = f"Cost_Adders = {Cost}")])
    arcpy.AddMessage(f"{Cost} Layer: {CostAdder_count}")

but receive the error below:

TypeError: 'where' is an invalid keyword argument for this function

Is this due to a bad where clause?

TomazicM
  • 25,601
  • 22
  • 29
  • 39
hBiery
  • 13
  • 4
  • 1
    The error says what the problem is, i.e., "invalid keyword." There is no parameter named where, the parameter is named sql_clause – bixb0012 Mar 08 '23 at 14:59
  • @Hornbydd this still gives me the same error – hBiery Mar 08 '23 at 15:01
  • @bixb0012 are you saying replace where with sql_clause? – hBiery Mar 08 '23 at 15:03
  • 1
    I see the problem now, you need to review your logic, you are summing on RR_Crossing but you are also querying the data on it as well as interstate and river fields which frankly makes no sense. – Hornbydd Mar 08 '23 at 15:07
  • @hBiery, yes, but after you fix the TypeError there will be logic-based errors as others have pointed out. – bixb0012 Mar 08 '23 at 15:16
  • @bixb0012 realized I didnt need the where clause at all, thank you – hBiery Mar 08 '23 at 15:24

1 Answers1

2

The same code structure in my answer for Error using ArcPy SearchCursor to get list of unique values in multiple fields - GIS SE can be used in this type of situation.

import arcpy
import collections

table = # path to feature class or table, or name of layer or table view fields = # list of field names to gather unique values

sums = collections.defaultdict(int) with arcpy.da.SearchCursor(table, fields) as cur: for row in cur: for i, value in enumerate(row): sums[cur.fields[i]] += value if value is not None else 0

for k, v in sums.items(): print(f"Sum of {k} is {v}")

bixb0012
  • 2,006
  • 1
  • 5
  • 18