I'm trying to assign speed values to street types using TIGER street shapefiles. So I created the new field for the speed, and I was going to use the field calculator to take the MTFCC classifications and fill the speed field from that, using a simple replace script.

Attribute table for shapefile

I have this simple script that I found from another question that someone asked. I plugged my fields and values in and repeated the replace.

def Calc(value):
if "S110" in value:
    return value.replace('S1100','65')
if "S1200" in value:
    return value.replace('S1200','45')
if "1400" in value:
    return value.replace('S1400','30')
if "S1500" in value:
    return value.replace('S1500','15')
if "S1630" in value:
    return value.replace('S1630','25')
if "S1640" in value:
    return value.replace('S1640','25')

enter image description here

However, when I run it, it seems to run fine, but my fields are still Null. I really have no experience with Python, so I'm not even sure where I'm going wrong with the script.

1 Answer 1


You are almost there. What you are trying to do is:


def calc_speed(field_value):
  if field_value == 'S1100':
    return 65
  if field_value == 'S1200':
    return 45

Speed field

Speed = 

You have to evaluate the value of MTFCC field and then make decision based on that.

Imho a more elegant solution is to use a Python dictionary. Less repetitive code and also easy to expand in case you get more class values.


d = {'S1100':65,'S1200':45,'S1400':30}

def calc_speed(field_value):
  if field_value in d.keys():
    return d[field_value]

Speed field

Speed = 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.