Select to view content in your preferred language

Using dictionary in field calculator

1699
6
Jump to solution
01-11-2024 01:21 AM
SSISC_FieldCrew
Occasional Contributor

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?

0 Kudos
1 Solution

Accepted Solutions
DavidWynne
Esri Contributor

@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:

DavidWynne_0-1705010375827.png

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.

 

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor

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

 

 

 


... sort of retired...
SSISC_FieldCrew
Occasional Contributor

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.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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
Occasional Contributor

Yes, the values match exactly and the capitalization also.

0 Kudos
DavidWynne
Esri Contributor

@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:

DavidWynne_0-1705010375827.png

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.

 

SSISC_FieldCrew
Occasional Contributor

Thanks very much @DavidWynne, that did the trick.