[GIS] Replacing multiple values in attribute table field using ArcGIS field calculator and python parser

arcgis-desktopfield-calculatorpython-parser

I'm new to python and trying to write a python script using the field calculator in ArcMap for a specific field in an attribute table that will replace some of the values in the field but leave the other values as they are within the table.

For example: ""fourth" must be changed to "4th" whereas "Neilston" should remain as it is in the field. The field in the table looks like this:

enter image description here

The python script used in the codeblock looks like this:

def streetNUM(field):

   if field == "fourth":

      field.replace ("fourth","4th")

   elif field == "fifth":

      field.replace ("fifth","5th")
   else:
      return field

And then in the box below the codeblock I have:

streetNUM(!FULLNAME!)

Nothing seems to change and I get an error code saying:

Field is not nullable [FULLNAME]

Any suggestions as to where I may be going wrong with my code?

Best Answer

You have a couple things going on in your IF and ELIF conditions:

 if field == "fourth":

This line will only be true if the value in your field is "fourth", in lowercase. "Fourth" wouldn't satisfy this condition, neither would "Fourth St." or "fourth street".

 field.replace ("fourth","4th")

You need to prefix this with return. Right now this line will replace "fourth" (again, only if it's lowercase) with "4th", but you need your function to return that value.