Using Python in Field calculator to update null values

788
5
04-22-2014 06:20 AM
LucindaKirkpatrick
New Contributor
I am trying to update some entries in an attribute table so that where I have blanks I now have information based on another field in the table using the following code:

    def firstupdate(DescGroup, DescTerm):
      if (DescGroup in ['Inland Water', 'Tidal Water']) and (DescTerm == <Null>):
        return 'Water'
      elif (DescGroup in ['Building' , 'Structure' , 'Glasshouse']) and (DescTerm == <Null>):
        return 'Structure'
      elif (DescGroup in ['Road' , 'Track' , 'Path', 'Rail' , 'Roadside']) and (DescTerm == <Null>):
        return 'Track'
      else DescTerm == <Null>:
        return 'Landform'

My problem is making sure I am recognising Null properly - I have tried with or without quotes as DescTerm is a string field but neither work and I get a syntax error. I have also just tried with using '' rather than the word Null.My attribute table has a field called DescTerm which has some blank entries which I want to update by what is in the DescGroup field.  I want all the ones that aren't blank to be left as they are!   Any advice on how to do this is much appreciated as this is my first foray into Python!
ps i have also tried doing this just by selecting the blank ones and running an abridged version of the code without the reference to null and I still get a syntax error or it runs and it doesnt actually do anything.
Tags (2)
0 Kudos
5 Replies
MathewCoyle
Frequent Contributor
DescTerm == <Null> 
is an invalid statement.

This should capture any blank or null entries in a string field.

if DescTerm in ("", " ", None):
    ...
Zeke
by
Regular Contributor III
Python doesn't recognize 'Null'. Instead, use None. Also, the proper way to check is 'is None', or 'is not None'. Without quotes, of course.
0 Kudos
LucindaKirkpatrick
New Contributor
Thank you - that worked, so it did replace the Null values - but it also then replaced all the other values as well that were already in there from before! Is there something about the way I have ended the script that caused it? This is the updated version of the script I used:

#DescGroup update
firstupdate(!DescGroup!, !DescTerm!)


def firstupdate(DescGroup, DescTerm):
  if (DescGroup in ['Inland Water', 'Tidal Water']) and (DescTerm in '', " ", None):
    return 'Water' #Needs to be string
  elif (DescGroup in ['Building' , 'Structure' , 'Glasshouse']) and (DescTerm in '', " ", None):
    return 'Structure' #all fields in DescGroup with Building / Structure / Glasshouse in catagorised as Building
  elif (DescGroup in ['Road' , 'Track' , 'Path', 'Rail' , 'Roadside']) and (DescTerm in '', " ", None):
    return 'Track'
  elif (DescTerm in '', " ", None):
    return 'Landform' #if blank and dont fit any other catagory
  else:
    return DescTerm  # if not blank

Also can you make python recognise text within a string so that 'Road' and 'Road; Path' are both relabelled Track without having to go through all the different catagories? I have many catagories like this that need to be lumped together and I thought that python would recognise just the letters in the string rather than the whole string having to match? Thank you!
0 Kudos
MathewCoyle
Frequent Contributor
You have your brackets off. You don't need the whole term in brackets just your listed items.

Good
DescTerm in ("", " ", None)

Bad
(DescTerm in '', " ", None)


For your second part the easiest way is using the in method.
if 'road' in DescGroup
0 Kudos
LucindaKirkpatrick
New Contributor
You have your brackets off. You don't need the whole term in brackets just your listed items.

Good
DescTerm in ("", " ", None)

Bad
(DescTerm in '', " ", None)


For your second part the easiest way is using the in method.
if 'road' in DescGroup


That worked, thank you! Now just to figure out the catagories...read somewhere that using regular expressions may help!
0 Kudos