Field Calculating with Python

721
4
07-13-2017 12:10 PM
KevinCross2
New Contributor III

Question regarding Python and field calculating.  I have 4 fields "BaseArea", "SW_Fee", "PT", "IASqFt".  I am trying to field calculate the "SW_Fee" field based on a fee structure.  My "PT" field has 3 catagories ( 'Residential', 'Industrial', 'Commercial') (more catagories could be added later).  My "BaseArea" and "IASqFT" are both long integers.

I need something along the line of " If "PT" equals 'Residential' then look in "BaseArea".  If "BaseArea" is >= to 1501 then the fee is $2.50 then if "BaseArea is <= 1500 the fee is $1.50.

BUT if "PT" equals 'Commercial' I have a whole other set of criteria for it to look at.  

Any and all help is appreciated.  

4 Replies
JoshuaBixby
MVP Esteemed Contributor

How are you wanting to implement this calculation?  A cursor?  Field Calculator?  If the latter, using the GUI or calling through Python?

Is your issue how to use a cursor or field calculator to do this, or is your issue more not knowing how to implement logic structures in Python?

KevinCross2
New Contributor III

My issue is not knowing how to implement logic structures.  I want to use the field calculator but I'm not sure how to implement it.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I recommend an update cursor as well.  If you really want to use the field calculator, use the GUI and don't try to call it from Python because doing so requires define the code block as a string, and I always find building nested logic structures as a string can get confusing for folks new to Python.

MitchHolley1
MVP Regular Contributor

Personally, I would use a cursor.

Something like this should be a start.

with arcpy.da.UpdateCursor(datasource, ['PT','BaseArea','SW_Fee','IASqFt']) as cursor:
    for row in cursor:
        if row[0] == 'Residential':
            if row[1] >= 1501:
                row[2] = '$2.50'
            if row[1] <= 1500:
                row[2] = '$1.50'
        elif row[0] == 'Commercial':
            print '__whole other set__'
        cursor.updateRow(row)
del cursor‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍