Python code to create a field based on another field. why is this not working?

1015
8
04-26-2012 10:18 AM
SudeshnaGhosh
New Contributor
I have one field in the attribute table called CO . This field has codes of counties like 017, 049, so on. Now I want to create a field called CO_NAME which will have names of counties based on county codes like "Bourbon" for 017, "Clark" for 049 and so on.


I go to field calculator, select python and check show codeblock

This is what I enter

def coname(CO):
  if CO==067:
   return "Fayette"
elif CO==209:
  return "Scott"
elif CO==239:
  return "Woodford"
elif CO==017:
  return "Bourbon"
elif CO==049:
  return "Clark"
elif CO==073:
  return "Franklin"
elif CO==081:
  return "Grant"
elif CO==097:
  return "Harrison"
elif CO==187:
  return "Owen"


CO_NAME =
coname(!CO!)

What is my mistake here? Thanks.
Sudeshna
Tags (2)
0 Kudos
8 Replies
MathewCoyle
Frequent Contributor
First, when posting code enclose in
[/ CODE] blocks to preserve formatting.


Second, it looks like your CO field is a string with numeric values, not a numeric field, so you need to enclose the values in quotes as you do your other strings.
0 Kudos
SudeshnaGhosh
New Contributor
Hi, I tried this again but not working.

Now I moved back to python tutorial (http://training.esri.com/Courses/PythonDesktop10_0/player.cfm) and worked on the Westerville file. While doing step 6: Create a multiline calculation expression, the code is not running.

This is exactly from the tutorial, I loaded the schools.cal expression

def label(name, type):
if type == "HIGH":
  return name + " HS"
if type == "MIDDLE":
  return name + " MS"
elif type == "ELEMENTARY":
  return name + " ELEM"

Map_Label =

label(!NAME!, !TYPE!)


I tried this several times. Is there any bug problem?
0 Kudos
SudeshnaGhosh
New Contributor
the errors are
Error 999999: Error executing function
The field is not nullable (MAP_LABEL)
Failed to execute (Calculate field)
0 Kudos
MarcinGasior
Occasional Contributor III
Sudeshna,
You're right. The code from VC don't work in AG10sp4. But I ran this code couple months earlier with no problems!
I supposr it's due to some problems with NULL values in fields which are used in Python code.

The workaround for VC is to select all records except those one with NULL in TYPE field.


As regards your first question, try this code (remember that indentation is important:
def coname(CO):
    if CO=='067':
        return "Fayette"
    elif CO=='209':
        return "Scott"
    elif CO=='239':
        return "Woodford"
    elif CO=='017':
        return "Bourbon"
    elif CO=='049':
        return "Clark"
    elif CO=='073':
        return "Franklin"
    elif CO=='081':
        return "Grant"
    elif CO=='097':
        return "Harrison"
    elif CO=='187':
        return "Owen"
0 Kudos
SudeshnaGhosh
New Contributor
Marcin, u r right with the code. Thanks. I did put quotes '067' later as the field was a string. So I guess my code is correct now. But anyways, is there a solution to the problem I am facing with running the code.
0 Kudos
Zeke
by
Regular Contributor III
If you're going to have a lot of elifs, sometimes it's easier to make a spreadsheet,txt or csv file with the data and join it to the existing table based on the common field, CO in this example. You can then either leave the join or export the whole thing to a new feature class or shapefile.
0 Kudos
LaviniaPoruschi
New Contributor
the errors are
Error 999999: Error executing function
The field is not nullable (MAP_LABEL)
Failed to execute (Calculate field)


Hi,

I found that the code worked only when the empty value in TYPE was edited as 'ELEMENTARY'. I put in a random text value, but that did not help. Apparently the Python parser can't deal with empty inputs, but also can't deal with inputs which are not on the if-else list of options.

Cheers from a beginner
0 Kudos
curtvprice
MVP Esteemed Contributor
Apparently the Python parser can't deal with empty inputs, but also can't deal with inputs which are not on the if-else list of options.


You left out an else clause. Though it's true that Calculate Field does have issues with null input if I recall, so this may not work with the null case.

def coname(CO):
    if CO == None:
         return "None"
    if CO == '067':
        return "Fayette"
    elif CO == '209':
        return "Scott"
    else:
        return CO
0 Kudos