If I understand the details of the question correctly (described in the comments), I think you should be able to do all of this in two steps using built-in geoprocessing tools, which you can of course also script in Python:
- Use Summary Statistics (Analysis), to create a table with the
mean value of field X in layer A. Specify the Case field to be the ID field that relates layer A to layer B.
- Use Join Field (Data Management), to permanently join the
mean value field from the result of step 1 to layer B.
Try it out interactively in ArcToolbox first, then in the Results window, right-click the results for each of the two steps and click "Copy As Python snippet", and paste them into a new Python script. Tweak as needed (read in parameters and assign to variables, use variables in the tool arguments instead of hard-coding them, etc).
Here is an example workflow, which hopefully you will find can be applied to many other problems.
I have a point feature class named
atlantic_hurricanes_2000 (from the ArcObjects SDK sample data) that has many points recording windspeed, pressure, etc., along the path of each hurricane from the 2000 hurricane season:
I created a polyline feature class from it for the purposes of demonstration (each line is a separate hurricane track). The only meaningful attribute on the polyline layer,
EVENTID is the name of the hurricane which will serve as an ID field that relates back to the point feature class:
Problem statement: The point feature class has a
WINDSPEED field recording the wind speed at each point of a given hurricane track. I want to take the mean
WINDSPEED value of all the points for each track in the point feature class and assign it to each polyline feature by adding a field.
I used Summary Statistics as follows:
Which produced this table:
I then used Join Field as follows:
Python snippet output: