Field calc help

561
2
06-18-2014 04:39 AM
BenjaminMittler
Occasional Contributor III
I'm trying to populate a field with an already defined area but only if the Updat_HYGR variable is = to 'A'

i have a column with letters A-D (Updat_HYGR), a Column with areas in sqmi (Sq_Miles) and a new column A_SqMi that i only want populated with the area if the Updat_HYGRP = 'A'

i just started using python but my statement looks like this

def Reclass(A_SqMi):
if (Updat_HYGR == 'A'):
  return Sq_Miles



Reclass(!A_SqMi!)


Any help would be appreciated
Tags (2)
0 Kudos
2 Replies
Zeke
by
Regular Contributor III
Couple notes: make sure the parser is set to Python (VBScript is the default), and Reclass isn't a good function name, because there's already a Reclass tool for rasters. Not saying it won't work, but at best it's confusing.

Anyway, you have to include the field(s) you use in the argument list.

def myUpdate(hygrp, sqmile):
    # using upper() in case value is lower case a
    if hygrp.upper()== 'A': return sqmile


myUpdate(!Updat_HYGRP!, !Sq_Miles!)


Another alternative is to just select by attribute features where Updat_HYGRP = 'A', then calculate those values to Sq_Miles. The Field Calculator will only calculate the selected records.
0 Kudos
BradPosthumus
Occasional Contributor II
In your function you need to feed in all of the fields you'll be using within the function to determine the return value. So your code should look something like this (after starting the field calculator on your "A_SqMi" field):
def Reclass(Updat_HYGR, Sq_Miles):
    if (Updat_HYGR == 'A'):
        return Sq_Miles


Reclass(!Updat_HYGR!, !Sq_Miles!)


As an added note, if your field is not nullable you'll need to include an else statement that sets it too a predetermined invalid value (e.g. else: return -1).
0 Kudos