Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited title
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Calculate Calculating fields (multiple) with different conditions for each field using ArcGIS Pro

edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Source Link
user74862
  • 131
  • 2
  • 5

Calculate fields (multiple) with different conditions for each field

I have a population dataset where I want to reclassify the age groups and include gender. I have the field "gender", 1 for men and 2 for women, and the field "age". One row for each person in the dataset.

I want 5-year age groups based on gender: Men 0-4 years (M_0_4), men 5-9 (M_0_9)...up to M95_99 and W_0_4, W_5_9 and so on. I can do this manually for each field with this code:

M_0_4=
    Reclass(!gender!, !age!)
    
    #codeblock:
    def Reclass(gender, age):
       if (gender = 1 and age >= 0 and age <= 4):
            return 1
        else:
            return 0

Is there anyway to do this for all fields using calculate field (multiple) in one go? Each field would need a different condition. I have tried making a separate expression for each field and then just copying the codeblock for each and changing the expression name and conditions without luck, e.g:

#expressions:
M_0_4=
  m_0_4(!gender!, !age!)
M_5_9=
  m_5_9(!gender!, !age!)

#codeblock:
def m_0_4(gender, age):
   if (gender = 1 and age >= 0 and age <= 4):
        return 1
   else:
        return 0

def m_5_9(gender, age):
   if (gender = 1 and age >= 5 and age <= 9):
       return 1
   else:
       return 0