# Calculate Field based on another field using Python

6158
6
08-03-2018 12:32 PM
New Contributor II

I am working on a TIGER line file of roads in my county in shapefile format (although I am having the same problem with geodatabase format). I am attempting to update a created field named SpeedMPH based on another field. If the field named ROAD_MTFCC is equal to S1200 then update the SpeedMPH field to 25. My code block is written in Python 3.6 and I am running ArcGIS Pro 2.2.1 with the latest updates. My code block is:

def Reclass(fld):
return 25
return 35
return 45
return 55

I then click the green check to verify my code and it is accepted. I click the run button and it runs for a few seconds. When I look at my TIGER line file of roads attribute table the SpeedMPH field is NULL. Nothing was updated.

What am I doing wrong?

Enclosed is a picture of the Calculate field window and a 7zip of my TIGER line shapefile. It is pretty standard Census data.

Tags (4)
1 Solution

Accepted Solutions
MVP Regular Contributor

``Reclass(!ROAD_MTFCC!)def Reclass(fld):    if(fld == 'S1200'):        return 25    #  etc.....‍‍‍‍‍‍‍‍‍``
6 Replies
MVP Regular Contributor

``Reclass(!ROAD_MTFCC!)def Reclass(fld):    if(fld == 'S1200'):        return 25    #  etc.....‍‍‍‍‍‍‍‍‍``
New Contributor II

Thank you Randy Burton.

That was very helpful. I am posting a screen grab of the answer as it is supposed to look.

MVP Esteemed Contributor

Although if/elif statements can be used to create simple mappings, the statements tend to get cumbersome for anything but the most trivial mappings.  The primary mapping type in Python is dict, and it makes for a much more straightforward function in your case:

``def Reclass(fld):    d = {'S1200':25, 'S1400':35, 'S1500':45, 'S1640':55}    return d.get(fld.upper())‍‍‍``

Using the get method takes care of the missing values being passed to the dictionary.

by
New Contributor III

Very Slick!  Thanks for the example Josh. Used it to convert some numeric domains to text.

``````def Reclass(fld):
d = {
'0':'Unclassified',
'1':'Cemetery',
'2':'Dock',
'3':'Park',
'4':'Parkin Lot',
'5':'Sign',
'6':'Stormwater Treatment Area',
'7':'Telecom',
'8':'Utility-Water',
'9':'Utility-Electric',
'10':'Utility-Sewer',
'11':'Utility-Gas',
'12':'Vacant',
'13.1':'Apartment',
'13.01':'Single Family Unit',
'13.02':'Duplex',
'13.03':'Triplex'
'13.05':'Mobile Home',
'13.06':'Townhouse',
'13.07':'Condominium',
'13.08':'Prefabricated Home',
'13.09':'Abandoned-Dilapidated',
'13.11':'Multi-Family',
'13.12':'Abandoned Mobile Home',
'14':'Commercial',
'15':'FSU',
'16':'TCC',
'17':'FAMU',
'18':'Elementary School',
'19':'Middle School',
'20':'High School',
'21':'Religious',
'22':'Airport',
'23':'Community Center',
'24':'Cultural',
'25':'General',
'26.01':'Govt-City',
'26.02':'Govt-County',
'26.03':'Govt-State',
'26.04':'Govt-Federal',
'27':'Hotel',
'28':'Industrial',
'29':'Medical',
'30':'Recreation',
'31':'Transportation',
'32':'Bar-Shed-Shop',
'33':'Delete',
'34':'New Building See Notes',
'35':'Garage Carport',
'36':'Mobile Home Storage',
'99':'Re-Review',
}
return d.get(fld.upper())``````

MVP Regular Contributor

Dictionary .get() is great! You can also specify a default value (besides None) if the key is not found.

MVP Regular Contributor

You may want to restructure your if/elif/else in your function so that it will always return something - perhaps a zero or 25 for any unknown value.

``if (fld == 'S1640'):  return 55elif (fld == 'S1500'):  return 45elif (fld == 'S1400'):  return 35else:  # for S1200 and any other value, just in case  return 25‍‍‍‍‍‍‍‍``