1

ArcMap 10.4.1 on Windows 7.

I am trying to fill in some blank rows in a column using field calculator. The field I am trying to fill is: label. If it is empty I want it to be populated with a value from the column: FULLNAME. If it is not blank I want it to retain its current value.

I have looked at other if else examples and this seems to be very similar, but my code doesn't work. I am also confused on when the exclamation points, example (!field!) are necessary if field calculator expressions.

Do those tell python to pull that field from the table?

def updateLabel (label, FULLNAME):
  if !label! is None:
     return !label!.replace( !FULLNAME! )

Do I need a code block in this case?

4

In fact you can use a one-liner in Codeblock part without using Pre-logic Script, e.g.:

!label! if !label! else !FULLNAME!

  • +1 nice solution – FelixIP Apr 25 '18 at 5:51
  • @fatih_dur I am trying to do this same trick with another data set !StatCause! if !StatCause! elif !Fire_Cause! elif !STAT_CAUSE_DESCR! else !Field8! It fills every field in StatCause with Null. Am I missing something in this Codeblock? – sparky May 10 '18 at 17:25
  • @sparky, this is a python trick, you can assign a value to a variable (the field value in codeblock, which ends with =) and it accepts only one else. If you want to make nested conditionals which gets the value if there is a truth value (which return True), you can use list comprehension, such as [a for a in [!StatCause!, !Fire_Cause!, !STAT_CAUSE_DESCR!, !Field8!] if a][0]. – fatih_dur May 11 '18 at 2:19
  • @sparky, Be aware, the first truth value will be assigned to the field. However, this does not handle if all your fields have a implied False value. You can filter them out by Select by Attribute tool. For truth value evaluation, see docs.python.org/2/library/stdtypes.html#truth-value-testing – fatih_dur May 11 '18 at 2:20
1

The exclamation points are used within the field names in the Field Calculator, but would not be needed within the function definition.

You can go two routes with this. Possibly the faster option (depending on the number of rows) would be to use Select By Attributes to select the rows with a blank Label field. Then use field calculator with the definition =!FULLNAME!. This will only process on the selected rows.

If you wish to use just the Field Calculator without first selecting the rows by attribute, you will need to use a code block. The code block's "Pre-Logic Script Code" would contain your function definition, e.g.

def updateLabel(lbl, fname):
    if lbl is None:
        return fname

For the actual field calcution, call the function with the fields as input.

updateLabel(!label!, !FULLNAME!)

The answer to this related question shows screenshots of the Field Calculator and code blocks, and gives a deeper explanation of the use of exclamation points within this tool.

  • Thank you smiller, that helped. I got the error message saying that the label field is not nullable. It is a string field, so how does one go about telling python to look for a blank string? I tried: if label == "" but I got the same error. I am getting both the 999999 and the field is not nullable error. Any ideas. – sparky Apr 24 '18 at 20:24
1

You have to include else and return something too for that condition, try:

def updateLabel(lbl, fname):
    if lbl == '':
        return fname
    else: 
        return lbl

or

def updateLabel(lbl, fname):
    if lbl is None:
        return fname
    else: 
        return lbl

The exclamation characters are required (in Field Calculator) when using the python parser, otherwise brackets are required when using the vb script parser.

enter image description here

0

Thanks artwork 21. I thought that you could just have an if statement and not provide an else. Guess I was wrong. For me here is the code that accomplished my initial question.

Pre-Logic Script Code:

def updateLabel(label, FULLNAME):
  if label == ' ' :
     return FULLNAME
  else:
    return label

CodeBlock:

updateLabel(!label!,!FULLNAME!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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