Converting state name abbreviations to full state names using ArcGIS Field Calculator

3235
2
08-19-2020 02:22 PM
AndresCastillo
MVP Regular Contributor
'''
The function is defined in the code block.
The function is executed in the expression.
Hit the 'verify' check mark to ensure the expression is valid.

After the run, check for remaining null values in the calculated field.
Nulls indicate your state abbreviations were not defined in the dictionary.
https://gis.stackexchange.com/questions/16423/converting-state-name-abbreviations-to-full-names-using-arcgis-field-calculator
'''

# Expression:
#  <FIELD> =
stateNames(!STATE_ID!)

# Code Block
def stateNames(stateAbbreviation):
    states = {
            'AK': 'Alaska',
            'AL': 'Alabama',
            'AR': 'Arkansas',
            'AS': 'American Samoa',
            'AZ': 'Arizona',
            'CA': 'California',
            'CO': 'Colorado',
            'CT': 'Connecticut',
            'DC': 'District of Columbia',
            'DE': 'Delaware',
            'FL': 'Florida',
            'GA': 'Georgia',
            'GU': 'Guam',
            'HI': 'Hawaii',
            'IA': 'Iowa',
            'ID': 'Idaho',
            'IL': 'Illinois',
            'IN': 'Indiana',
            'KS': 'Kansas',
            'KY': 'Kentucky',
            'LA': 'Louisiana',
            'MA': 'Massachusetts',
            'MD': 'Maryland',
            'ME': 'Maine',
            'MI': 'Michigan',
            'MN': 'Minnesota',
            'MO': 'Missouri',
            'MP': 'Northern Mariana Islands',
            'MS': 'Mississippi',
            'MT': 'Montana',
            'NA': 'National',
            'NC': 'North Carolina',
            'ND': 'North Dakota',
            'NE': 'Nebraska',
            'NH': 'New Hampshire',
            'NJ': 'New Jersey',
            'NM': 'New Mexico',
            'NV': 'Nevada',
            'NY': 'New York',
            'OH': 'Ohio',
            'OK': 'Oklahoma',
            'OR': 'Oregon',
            'PA': 'Pennsylvania',
            'PR': 'Puerto Rico',
            'RI': 'Rhode Island',
            'SC': 'South Carolina',
            'SD': 'South Dakota',
            'TN': 'Tennessee',
            'TX': 'Texas',
            'UT': 'Utah',
            'VA': 'Virginia',
            'VI': 'Virgin Islands',
            'VT': 'Vermont',
            'WA': 'Washington',
            'WI': 'Wisconsin',
            'WV': 'West Virginia',
            'WY': 'Wyoming'
    }
    if stateAbbreviation is not None:
        if stateAbbreviation in states:
            return states[stateAbbreviation]
        else:
            return None    
    else:
        return None
2 Replies
jackolcott
New Contributor II

Thank you, this was very useful. 

I was getting an error dropping your exact code into the calculate field tool, so I modified the return statements to return "N/A" instead of None and that seemed to fix the error. I imagine using None works too, but for whatever reason ArcPro was throwing me an error. I used your exact code and then changed the return statements to the following: 

if stateAbbreviation is not None:
        if stateAbbreviation in states:
            return states[stateAbbreviation]
        else:
            return "N/A"    
    else:
        return "N/A"

 Thanks again

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If the field does not allow NULL, then returning None would create an error since Python None gets converted to SQL NULL.