# Using Python in the Field Calculator, how do I refer to a field in another shapefile?

I want to compare the changes in geometry between two shapefiles (of election districts in NYC. One is from 2001 and the other 2005). I am attempting to calculate a new field in the 2005 shapefile that shows the change in AREA from the 2001 shapefile to the 2005 shapefile, using the districts common IDs.

What I can't seem to figure out is how to refer to a field name in ANOTHER shapefile in my field calculator definition.

Here's what I have so far:

Pre-Logic Script Code:

``````def areaChg(shpOrig, areaOrig, shpNew, areaNew):
origRows = arcpy.SearchCursor(shpOrig)
for origRow in origRows:
newRows = arcpy.SearchCursor(shpNew)
for newRow in newRows:
if origRow.ED == newRow.ED:
return newRow.ED - origRow.ED
else: pass
``````

AreaChg =

``````areaChg("nyed_01bav", areaOrig, "nyed_05", !Area!)
``````

It's the "areaOrig" in that last part of code, which is the place-holder for the field in the other shapefile, which I'm having a hard time with. How do I point to a field in another shapefile in field calculator? For the field in the shapefile from within which I'm doing the calculation, the correct format is "!Area!", but what about for a field from another shapefile?

Thanks!

-
Why can't you join the two shapefile tables together? Then you can reference the joined fields to do your calculation. – Baltok Feb 7 '13 at 18:57
Fantastic idea!! – Kristen G. Feb 7 '13 at 19:07
I guess I was really over-complicating such a simple problem, but I'm still curious how you would refer to a field in another shapefile in the field calculator (using Python) – Kristen G. Feb 7 '13 at 19:19
Once you have joined your tables, you should be able to reference the fields by using your shapefile name + the field name. E.g., !myshape.AREA!. – Baltok Feb 7 '13 at 19:37
Personally I would prefer to use a Python script tool for this but I think @Baltok suggestion well worth trying because here it says: "When calculating joined data, you cannot calculate the joined columns directly. However, you can directly calculate the columns of the origin table." – PolyGeo Feb 7 '13 at 21:33

Short Answer: Directly You can't.

Long Answer: Join the layer that contains the inputfield, to the layer that contains the field your want to calculate. After this, you can definetly use the input field in your Python Code.

-