In ArcPro 3.2.1, I'm trying to use the Calculate Field Tool within Model Builder to update the values of a field that has several different values. For example, the field currently contains the values of AVE, BLVD, CIR, CT, HWY etc. I want to update these values to the fully spelled out versions: AVENUE, BOULEVARD, CIRCLE, COURT etc. Is there a more efficient way to do this than selecting and calculating each value individually? This will be only one piece of a larger model and I don't want to have 30 select/calculates in the model -- it will be a mess! Thanks in advance.
I just can't get it to work. My problem is I don't know anything about code so it's like I'm trying to read before learning the alphabet. I do appreciate all the help!
Can you post another screenshot or sample again? Also, a screenshot of your table would help.
Truth be told, I find Python Field Calculator much more confusing than the Arcade one, but here's a rough explanation of how it works.
The "AVDIR =" block is there for you to put in a value. That could be a static value ADVIR =2 or it can be a dynamic one based off of one or more functions, which is what we're doing now ADVIR = myfunc(!FieldA!)
A function is a set of steps you define to accomplish a goal of yours. They are used to avoid having to retype your code a billion times, and we write them out in the code block. The code block can contain multiple functions, depending on your needs.
Functions are defined using the pattern def functionName(parameter1, parameter2...): and end with a return statement, which tells the computer that 1) the function is done and 2) whether or not it needs to send a value back to whatever called the function in the first place. Some functions do not take any parameter, some take 20. It just depends on your needs.
For example, we could make a function to make a peanut butter sandwich. We feed the function peanut butter, bread, and a knife, and we get a sandwich out of it.
# Define your function
def pbSandwich(peanutButter, bread, knife):
sandwich = 1 slice of bread # start with one piec
knife = knife+ peanutButter # put the peanut butter on the knife
sandwich = sandwich + knife # put the spread on the bread
sandwich = sandwich + 1 slice of bread # close the sandwich
return sandwich
# Call your function
lunch = pbSandwich(peanutButter, bread, knife)There's more context missing in this code (what is a knife? how did we get it?) but the important part is that we have instructions telling us how to make a sandwich whenever we have the right ingredients.
Applying that to the field calculator and the code we've given you,
def expandStreets(streetSuf):
suffixes = {'AVE': 'AVENUE',
'BLVD': 'BOULEVARD',
'CIR': 'CIRCLE',
'CT': 'COURT',
'HWY': 'HIGHWAY',
}
# Find me the matching suffix for what I feed it.
# If it's not in the list, just give me what I already have
# So if streetSuf == 'AVE', I get 'AVENUE', but
# if it equals 'PL', I just get 'PL' since it isn't in the dictionary
expnd = suffixes.get(streetSuf, streetSuf)
return expndAnd then we use the AVDIR = block to feed it the value for each record in your table of a given field. Not sure of the reason the syntax is !FieldA!, but that's how it's set up, so that's what we're doing.
Anyway, please share your errors and we'll figure it out together
So below is a partial screenshot of my table with the field AVDIR highlighted. I decided to start on this field since it has fewer values (only 4 - N, S, E, W) which I need fully spelled out (NORTH, SOUTH ETC). Below that is what I currently have in the Code Block which is all wrong; I know. Your explanation/analogy is great, I'm just not sure how to apply it.
Goes back to the earlier issue of calling the dictionary and the function the same thing.
You want to return compassDict[oldValue]