Python in Field Calculator

512
2
12-13-2012 11:22 AM
WilliamKaptein
New Contributor
I'm using the python example named on http://resources.arcgis.com/en/help/main/10.1/index.html#/Calculate_Field_examples/005s0000002m00000...

Parser:
Python

Expression:
Reclass(!WELL_YIELD!)

Code Block:
def Reclass(WellYield):
  if (WellYield >= 0 and WellYield <= 10):
    return 1
  elif (WellYield > 10 and WellYield <= 20):
    return 2
  elif (WellYield > 20 and WellYield <= 30):
    return 3
  elif (WellYield > 30):
    return 4


But i want to use this with 4 categories. So a wellyield with value 8 and category 1 gets a 1 return but a wellyield with value 8 and category 2 gets a 5 return and so on...

For example:
def Reclass(WellYield):
  if (Category = 1 and WellYield >= 0 and WellYield <= 720):
    return 10
  elif (Category = 1 and WellYield > 721 and WellYield <= 1085):
    return 20
  elif (Category = 1 and WellYield > 1086 and WellYield <= 1450):
    return 30
  elif (Category = 1 and  WellYield > 1451):
    return 40

  if (Category = 2 and WellYield >= 0 and WellYield <= 720):
    return 20
  elif (Category = 2 and WellYield > 721 and WellYield <= 1085):
    return 30
  elif (Category = 2 and WellYield > 1086 and WellYield <= 1450):
    return 40
  elif (Category = 2 and WellYield > 1451):
    return 50

  if (Category = 3 and WellYield >= 0 and WellYield <= 720):
    return 30
  elif (Category = 3 and WellYield > 721 and WellYield <= 1085):
    return 40
  elif (Category = 3 and WellYield > 1086 and WellYield <= 1450):
    return 50
  elif (Category = 3 and WellYield > 1451):
    return 60

  if (Category = 4 and  WellYield >= 0 and WellYield <= 720):
    return 40
  elif (Category = 4 and  WellYield > 721 and WellYield <= 1085):
    return 50
  elif (Category = 4 and  WellYield > 1086 and WellYield <= 1450):
    return 60
  elif (Category = 4 and  WellYield > 1451):
    return 70


But of course this one doesnt work. How do i get it to work ?

Thx in advance
Tags (2)
0 Kudos
2 Replies
MathewCoyle
Frequent Contributor
Is Category another field? If so you will need to pass it to the function.
0 Kudos
BruceBacia
Occasional Contributor
In your previous code you don't account for well yield values of 721, 1086, or 1451
How about something like this:

def reclass(category,WellYield):
    try:
        if WellYield >= 0 and WellYield <= 720:
            yld = 1
        if WellYield >= 721 and WellYield <= 1085:
            yld= 2
        if WellYield >= 1086 and WellYield <= 1450:
            yld = 3
        if WellYield >= 1451:
            yld = 4
            
        catYield = ''.join([str(category),str(yld)])       
        
        reclassDict = {'11':10,'12':20,'13':30,'14':40,
                       '21':20,'22':30,'23':40,'24':50,
                       '31':30,'32':40,'33':50,'34':60,
                       '41':40,'42':50,'43':60,'44':70}
        
        value = reclassDict[catYield]
        return value
    except:
        pass


Expression
reclass(!CATEGORY!,WELL_YIELD!)
0 Kudos