I am working with the field calculator in ArcPro. I have created a dictionary where the Key is an existing value in a field and the value is the one I would like to replace the existing value with. Below is a portion of my dictionary.
plant_dictionary = {
"ABUT THE": "Velvet leaf",
"ACRO REP": "Russian knapweed",
"AEGI CYL": "Jointed goatgrass",
"AEGO POD": "Goutweed / bishop's weed",
"AILA ALT": "Tree of heaven",
"ALHA MAU": "Camel thorn",
"ALLI PET": "Garlic mustard"
}
then I have this bit of code where INV_SP_PLA is the field whose data I am trying to replace.
for r in plant_dictionary:
if !INV_SP_PLA! in r:
!INV_SP_PLA!.replace(r, plant_dictionary[r])
else:
pass
ArcPro does not like my dictionary for some reason and I am unsure of what to put in the Expression portion of the field calculator.
Any ideas why this might not be working?
Solved! Go to Solution.
@SSISC_FieldCrew, to start, I would recommend updating a new field, at least until you're sure you have the logic the way you want.
To follow along with what Dan showed, I tried the following:
Expression:
plant(!INV_SP_PLA!)
Code Block:
plant_dictionary = {
"ABUT THE": "Velvet leaf",
"ACRO REP": "Russian knapweed",
"AEGI CYL": "Jointed goatgrass",
"AEGO POD": "Goutweed / bishop's weed",
"AILA ALT": "Tree of heaven",
"ALHA MAU": "Camel thorn",
"ALLI PET": "Garlic mustard"
}
def plant(val):
"""Demo"""
if val in plant_dictionary.keys():
new_val = plant_dictionary[val]
return new_val
else:
return val
Given the below table, the updated values are shown in the `new` field:
I included an `else` condition to return the original value if it's not in the dictionary. Like Joshua mentioned, variations in the original field values would result in some values not being updated.
expression
plant(!INV_SP_PLA!)
code block
def plant(val):
"""Demo"""
plant_dictionary = {
"ABUT THE": "Velvet leaf",
"ACRO REP": "Russian knapweed",
"AEGI CYL": "Jointed goatgrass",
"AEGO POD": "Goutweed / bishop's weed",
"AILA ALT": "Tree of heaven",
"ALHA MAU": "Camel thorn",
"ALLI PET": "Garlic mustard"
}
for r in plant_dictionary:
if val in r:
val.replace(r, plant_dictionary[r])
return val
Thanks for the prompt reply @DanPatterson. Rather than replace the key "ABUT THE" with the value "Velvet leaf" the code keeps the field values the same.
Are the values exactly the same between the dictionary key and the field in the table? If there is extra text with the value in the field in the table, including any spaces, the match won't happen. Also, does capitalization match? It would help to provide sample data.
@SSISC_FieldCrew, to start, I would recommend updating a new field, at least until you're sure you have the logic the way you want.
To follow along with what Dan showed, I tried the following:
Expression:
plant(!INV_SP_PLA!)
Code Block:
plant_dictionary = {
"ABUT THE": "Velvet leaf",
"ACRO REP": "Russian knapweed",
"AEGI CYL": "Jointed goatgrass",
"AEGO POD": "Goutweed / bishop's weed",
"AILA ALT": "Tree of heaven",
"ALHA MAU": "Camel thorn",
"ALLI PET": "Garlic mustard"
}
def plant(val):
"""Demo"""
if val in plant_dictionary.keys():
new_val = plant_dictionary[val]
return new_val
else:
return val
Given the below table, the updated values are shown in the `new` field:
I included an `else` condition to return the original value if it's not in the dictionary. Like Joshua mentioned, variations in the original field values would result in some values not being updated.
Thanks very much @DavidWynne, that did the trick.