Since I realised Python is a very useful tool in ArcMap I don't want to do any workarounds any more. I have a field with is numeric, but I want to perform this calculation in the field calculator on it

def change(GRIDCODE):
    if GRIDCODE == "1":
      return "XZ"
    elif GRIDCODE == "3":
      return "XY"
    elif GRIDCODE == "4":
      return "XX"
      return "None"

But, the field is a numeric one and it won't let me change it to a string. I suppose I have to change the parameter of the field first. Of course I could add a field, set the parameter of that field to string and then run the code above, a little bit modified, on the shapefile.

But is there a solution where I change the parameter of the field within the code?

  • Are you saying that the field you are trying to store the value into is numeric or that the field that GRIDCODE is coming from is numeric (or both)? Commented Jun 25, 2014 at 14:05
  • The values I try to replace are in the field (column) GRIDCODE and the field GRIDCODE comes from a reclassified Rasterfile and is numeric. Now I want to change values (all of them) from the field GRIDCODE to stringvalues in die field GRIDCODE. All is suppose to happen in one field.
    – four-eyes
    Commented Jun 25, 2014 at 14:07

2 Answers 2


If I understand correctly, you want to take an integer field and do some calculations and change the field to a text field, correct?

You cannot do that in the field calculator. However, you can do it in the python window. You would be adding another field, moving and converting values, and then deleting and replacing your GRIDCODE field with another that is text. Be sure you back up before you do anything in case there is an error in my code or your copying. Double check the parameters for the arcpy function as it is coming from my memory.

Update Cursor: http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000064000000

Add Field: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000047000000

Delete Field: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000052000000

What you would be looking to do would be something like this:

arcpy.AddField_management("TempField", "TEXT") #create a temp field to hold text values

rows = arcpy.UpdateCursor("YOURLAYER") #create an update cursor to convert and move values
for row in rows: #set an iteration for your rows
    row.TempField = str(row.GRIDCODE) #copy the value over
    rows.updateRow(row) #tell the cursor to update

arcpy.DeleteField_management("GRIDCODE") #remove your integer field
arcpy.AddField_management("GRIDCODE", "TEXT") #create another one with text
#mind you, this moves the field all the way to the right
rows = arcpy.UpdateCursor("YOURLAYER") #create a second update cursor
for row in rows: #set up another iteration
    if row.TempField == "1": #if statement for assignment
       row.GRIDCODE = "BSM"
    elif row.TempField == "3":
       row.GRIDCODE = "BSW"
    elif row.TempField == "4":
       row.GRIDCODE = "BSP"
       row.GRIDCODE = "None"
    rows.updateRow(row) #update your new text row
arcpy.DeleteField_management("TempField") #clean up the temporary field
  • Hey. Thanks a lot. I get my feed wet with that. I am not a python expert but I try to understand and learn :) thanks!
    – four-eyes
    Commented Jun 25, 2014 at 14:27
  • You can do it outside of python just using the attribute table. When you convert the interger to number in the field calculator in the temp field, make sure you set it up like str(GRIDCODE) and make sure python is checked. If you leave VBScript checked, I believe it is CStr(GRIDCODE). Then you can use your python field calculator function when you're resetting the next GRIDCODE (text) field.
    – Branco
    Commented Jun 25, 2014 at 14:29
  • You will have to write your string to a text field.
    – artwork21
    Commented Jun 25, 2014 at 14:36

If I understand your question, you don't need to calculate or program it. Just make a table like this:

1        BSM
3        BSW
4        BSP

and use "Join Field" tool to join it permanently onto your attribute table. That will add the field STRINGVAL to your attribute table and populate it with the values that correspond to GRIDCODE values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.