Select to view content in your preferred language

Calculate Field (reclassifying numeric to numeric) Python code not working

772
4
Jump to solution
11-10-2025 12:29 PM
RachelHough
New Contributor

I'm fairly certain that this is a VERY simple issue but I cannot figure it out as I'm a beginner. I'm trying to use a Python code to populate a new field in the attribute table based on values in another. I've added a new numeric field called BusTypeNumeric2 and would like to reclassify the existing numeric field BusTypeNumeric to populate it. BusTypeNumeric ranges from 1 to 5, and I would like to reclassify so that 1=1, 2=1, 3=2, 4=2, and 5=3. 

My expression(?) is:

BusTypeNumeric2=reclass(!BusTypeNumeric!)

The code block is:

def reclass(BusTypeNumeric):
    if (BusTypeNumeric == '1'):
        return '1'
    elif (BusTypeNumeric == '2'):
        return '1'
    elif (BusTypeNumeric == '3'):
        return '2'
    elif (BusTypeNumeric == '4'):
        return '2'
    elif (BusTypeNumeric == '5'):
        return '3'

 

When I verify the expression it comes back valid and when I run it it says that the operation was completed, but it doesn't change anything in the target field; BusTypeNumeric2 values are still all <Null> afterward. I've heard that if the fields are numeric, then you can use one = instead of two, but when I use one =, the code block comes up as invalid and I'm told to use two.

Any insight as to what I may be doing wrong? I'm happy to provide more information! 

0 Kudos
2 Solutions

Accepted Solutions
CodyPatterson
MVP Regular Contributor

Hey @RachelHough 

I think the only issue really is that when you're entering the function in the calculate field, it should be something like reclass(!BusTypeNumeric!) like this:

CodyPatterson_0-1762806863554.png


You may also want to try and check for the integer rather than the string '1' would be a string, something like this, just depends on if they are Text or not, I'd try both:

def reclass(BusTypeNumeric):
    if BusTypeNumeric == 1:
        return 1
    elif BusTypeNumeric == 2:
        return 1
    elif BusTypeNumeric == 3:
        return 2
    elif BusTypeNumeric == 4:
        return 2
    elif BusTypeNumeric == 5:
        return 3

The difference between = and == is a single = is for assignment, so test = 1, and the == is for an equivalency check, so if test == 1 which it does then it passes the if statement, in Python you can also leave out the parenthesis around the conditions!

Let me know if that helps!

Cody

View solution in original post

0 Kudos
DanPatterson
MVP Esteemed Contributor

Confirm you field types.  That is, the source field BusTypeNumeric, is it a text field or an integer type field?

The following shows the variants.

# -- checks for a text representation of the integer 1
BusTypeNumeric == '1'
# -- checks for the integer 1
BusTypeNumeric == 1

The destination field can be either text or numeric, but you can't put '1' in a numeric field, it has to be 1


... sort of retired...

View solution in original post

0 Kudos
4 Replies
CodyPatterson
MVP Regular Contributor

Hey @RachelHough 

I think the only issue really is that when you're entering the function in the calculate field, it should be something like reclass(!BusTypeNumeric!) like this:

CodyPatterson_0-1762806863554.png


You may also want to try and check for the integer rather than the string '1' would be a string, something like this, just depends on if they are Text or not, I'd try both:

def reclass(BusTypeNumeric):
    if BusTypeNumeric == 1:
        return 1
    elif BusTypeNumeric == 2:
        return 1
    elif BusTypeNumeric == 3:
        return 2
    elif BusTypeNumeric == 4:
        return 2
    elif BusTypeNumeric == 5:
        return 3

The difference between = and == is a single = is for assignment, so test = 1, and the == is for an equivalency check, so if test == 1 which it does then it passes the if statement, in Python you can also leave out the parenthesis around the conditions!

Let me know if that helps!

Cody

0 Kudos
RachelHough
New Contributor

Thank you!!! All I had to do was remove the apostrophes from the numbers as you demonstrated. This is a bit confusing to me by since the single = didn't work; I thought that double = were used with text values, which led me to believe that the apostrophes needed to be used with values. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

In python    =   the assignment statement

                   ==   the equality check


... sort of retired...
0 Kudos
DanPatterson
MVP Esteemed Contributor

Confirm you field types.  That is, the source field BusTypeNumeric, is it a text field or an integer type field?

The following shows the variants.

# -- checks for a text representation of the integer 1
BusTypeNumeric == '1'
# -- checks for the integer 1
BusTypeNumeric == 1

The destination field can be either text or numeric, but you can't put '1' in a numeric field, it has to be 1


... sort of retired...
0 Kudos