AnsweredAssumed Answered

Using Python Dictionaries to Assign Feature Values

Question asked by NatiNati1186 on Apr 23, 2020
Latest reply on Apr 23, 2020 by NatiNati1186



I am trying to use python dictionaries to change the feature values of different fields.

I created the script below to change the feature values of a field called "DOR_UC" and as you can see there is a list of other fields where I need to do something similar - that's why there is a long list of other fields.

import arcpy

# script to assign values to features in one field based on a value in the dictionary

arcpy.env.workspace = r"D:\APRX_MXDS\USA_Parcels_2019_Project\test_featureclasses.gdb"
arcpy.env.overwriteOutput = True

# feature class
fc = "testing_fc"

# printing all fields in fc
fields = [ for f in arcpy.ListFields(fc)]

# dictionary
uses = {"000" : "Vacant Residential – with/without extra features", "001" : "Single Family", "002": "Mobile Homes",
            "003" : "Multi-family - 10 units or more",
            "004" : "Condominiums",
            "005" : "Cooperatives",
            "006" : "Retirement Homes not eligible for exemption",
            "007" : "Miscellaneous Residential (migrant camps, boarding homes, etc.)",
            "008" : "Multi-family - fewer than 10 units",
            "009" : "Residential Common Elements/Areas",
            "010" : "Vacant Commercial - with/without extra features",
            "011" : "Stores, one story",
            "012" : "Mixed use - store and office or store and residential combination",

# dictionary keys to list
usesKey = list(uses.keys())
# dictionary values to list
usesvalues = list(uses.values())

# List of fields

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if usesKey in row[1]:
            row[2] = usesvalues

 Field DOR_UC currently has some codes that I need to convert into a sentence.

See image below for fc attribute's table:

Each of those codes has a meaning in the form of a sentence. I need to assign their meaning and delete the codes.


To do this I've created a dictionary where the key is the code and the value is the sentence (See attached csv).


To start testing the script I created a field called "TEST" where I want to put the value part of the dictionary.

So the code would go something like this:

If a feature in field DOR_UC is equal to 000 then the feature value in field TEST would be "Vacant Residential  - with/without extra features".


When I try to run the code I am getting this first error:

Traceback (most recent call last):
  File "D:/APRX_MXDS/USA_Parcels_2019_Project/Scripts/Dictionary Key()", line 44, in <module>
    for row in cursor:
RuntimeError: A column was specified that does not exist.

The weird thing is that I have both of the columns.

Not sure how to solve this.


Also, not sure if the overall code has more errors.


Could someone help me with this? I need to finish this today and I have no idea on how to continue.

thank you!