2

I am using arcpy.CalculateField_management to fill a column field with another column values.

I have two shapefiles, fishnet.shp and new_run.shp. First I joined both using arcpy.AddJoin_management as follows:

arcpy.MakeFeatureLayer_management("fishnet.shp","fishnet_layer") arcpy.AddJoin_management("fishnet_layer","FID","new_run_grid.shp","FID_fishne")

Now I want to assign fishnet.shp's column rungrid the value of the joined table's column final_run value.

How to use arcpy.CalculateField_management here?

I tried the following code but it gave error:

arcpy.CalculateField_management("fishnet_layer","rungrid","!new_run.final_run","PYTHON")

I am getting the following error:

ExecuteError: Failed to execute. Parameters are not valid. ERROR 000728: Field rungrid does not exist within table Failed to execute (CalculateField).

Further

I checked the names of the fields of fishnet_layer and these two names are there fishnet.run_grid and new_run.final_run. After giving the following command:

arcpy.CalculateField_management("fishnet_layer",'fishnet_f.run_grid','new_run_grid.run_grid1',"PYTHON")

I am getting the following error:

ExecuteError: ERROR 000539: Error running expression: new_run_grid.run_grid1 Traceback (most recent call last): File "<expression>", line 1, in <module> NameError: name 'new_run_grid' is not defined Failed to execute (CalculateField).

4
  • 1
    Check the field names using: print [f.name for f in arcpy.ListFields("fishnet_layer")] after join. Maybe you are using the wrong names in Calculate Field Commented Mar 14, 2019 at 11:21
  • @BERA I just did it and still got an error, I have edited the question, pl see Commented Mar 14, 2019 at 13:26
  • no time to test today, but I think that you must use the name of the layer, not the name of the table/feature class. So "fishnet_layer.rungrid", and for the second part you still need the !! Commented Mar 14, 2019 at 13:33
  • You need to also create a layer of new_run_grid.shp and use this in the join. You cant join a shapefile to a layer, both need to be layers Commented Mar 14, 2019 at 13:58

2 Answers 2

3

Check:

  1. You need to create layers of both shapefiles and use in the join. You are only creating one layer at the moment.
  2. Make sure you are using the correct field names in calculate field, for example with print [f.name for f in arcpy.ListFields("fishnet_layer")] after the join

I would use a Dictionary instead of trying to join the data and da.UpdateCursor instead of Field Calculator:

import arcpy
arcpy.env.workspace = r'C:\shapefilefolder'

shape_to_update = 'fishnet.shp'
idfield_update = 'FID'
field_to_update = 'rungrid'

shape_to_read = 'new_run_grid.shp'
idfield_read = 'FID_fishne'
field_to_read = 'final_run'

d = {k:v for k,v in arcpy.da.SearchCursor(shape_to_read,[idfield_read,field_to_read])}
with arcpy.da.UpdateCursor(shape_to_update,[idfield_update,field_to_update]) as cursor:
    for row in cursor:
        if row[0] in d:
            row[1] = d[row[0]]
            cursor.updateRow(row)
0

You're missing a !

You have "!new_run.final_run"

Should be "!new_run.final_run!"

1
  • This is not an answer, but rather a comment on a syntax issue with OP's code. It does not solve this question. Commented Mar 19, 2019 at 16:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.