Select to view content in your preferred language

Replacing Multiple Fields using Python in Field Calculator

533
2
Jump to solution
08-16-2023 02:29 PM
MatthewElbert1
Occasional Contributor

I am attempting to take multiple field attributes with a specific classification/moniker (in this case-- any attribute field containing a value with SAS-06-xxxxx), and replace them with classification more in line with the rest of the attributes (Ascending sequential values up from SAS-02-05700 as a starting point).  What would the python syntax look like in the code block using Field Calculator.  Novice with Python, so any help/feedback is appreciated.  Thx.

 

 

ESRI Community Field Calc Syntax.jpg

1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Select the unwanted rows with Select Layer By Attribute or with a definition query

JohannesLindner_0-1692222211782.png

 

Use this with Calculate Field (This changes the data, make a backup!)

# ID =
next_sas_02_sequence()

# Code Block
sas_counter = 5700

def next_sas_02_sequence():
    global sas_counter
    sas_02_sequence = f"SAS-02-{sas_counter:05}"
    sas_counter += 1
    return sas_02_sequence

 

JohannesLindner_1-1692222346059.png

 


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

Select the unwanted rows with Select Layer By Attribute or with a definition query

JohannesLindner_0-1692222211782.png

 

Use this with Calculate Field (This changes the data, make a backup!)

# ID =
next_sas_02_sequence()

# Code Block
sas_counter = 5700

def next_sas_02_sequence():
    global sas_counter
    sas_02_sequence = f"SAS-02-{sas_counter:05}"
    sas_counter += 1
    return sas_02_sequence

 

JohannesLindner_1-1692222346059.png

 


Have a great day!
Johannes
MatthewElbert1
Occasional Contributor

Thanks Johannes, this was very easy to follow and navigate.  I tested on a dummy set first, and it worked!  Much appreciated with the feedback and screenshots. You just reinforced my belief I need to brush up/build on my Python skills. Cheers!

0 Kudos