Select to view content in your preferred language

Attribute table update using standalone table

06-13-2023 03:39 AM
New Contributor

I have a river gauge point layer whose attribute table has gaugid and average_reading fields. I also have a standalone table the has morning and evening readings field for each river gauge. The standalone table is fed readings using Field maps twice a day. I have published the layers on ArcGIS online.

I want a script that gets daily average for each gauge and then updates the average reading to the point attribute table in the average_reading field.

0 Kudos
1 Reply
MVP Frequent Contributor

Untested, supply your table and field names in lines 13 & 17.

import arcpy

def mean(values):
    values = [v for v in values if v is not None]
        return sum(values) / len(values)
    except ZeroDivisionError:
        raise ValueError("No non-null values in input list!")

# get the morning and evening readings from the update table (dict with id as key)
update_rows = {
    i: [m, e]
    for i, m, e in arcpy.da.SearchCursor("UpdateTable", ["GaugeID", "Morning", "Evening"])

# update the gauge layer
with arcpy.da.UpdateCursor("Gauges", ["GaugeID", "AverageReading"]) as cursor:
    for i, avg in cursor:
            avg = mean(update_rows[i])
        except KeyError:
            print("GaugeID not found in update table: " + str(i))
        except ValueError:
            print("Could not calculate average for GaugeID " + str(i))
        cursor.updateRow([i, avg])

Have a great day!
0 Kudos