Select to view content in your preferred language

How do I make a new field based on an existing field?

1194
6
04-13-2012 03:28 AM
ElizabethLewis
Emerging Contributor
Hi,

I have a shapefile of the geology of the UK that contains lots of polygons for the different types of rocks. I want to group some of these polygons together but I'm not sure how.

There is a field called "Class" in my existing file and I want to create a new field called "NewClass". I basically want to do an if statement:

IF "Class" = '1A' or '2A', "NewClass" = 1
IF "Class" = '1B' or '2B', "NewClass" = 2
IF "Class" = '1C' or '2C' or '3', "NewClass" = 3

How do I do this in Arc10 (windows 7)

Thanks
Tags (2)
0 Kudos
6 Replies
MarcinGasior
Frequent Contributor
In Field Calculator for 'NewClass' field chose Pytho parser, click Show Codeblock.
Use the following code in Pre-Logic Script Code:
def reclass(oldClass):
    if oldClass in ('1A', '2A'):
        return '1'
    elif oldClass in ('1B', '2B'):
        return '2'
    elif oldClass in ('1C', '2C', '3'):
        return '3'

then enter:
NewClass =
reclass(!Class!)
0 Kudos
SudeshnaGhosh
Emerging Contributor
Hi I have similar uses, but mine is a little different.

I want to create a Speed limit field from existing road type field.

If road_type = US, then Speed_limit=55
if road_type = I, then Speed_Limit=70
a
and so on.

I would appreciate any help.

Thanks
0 Kudos
MarcinGasior
Frequent Contributor
@Sudeshna,
The code is very similar:
def speed(roadType):
 if roadType == 'US':
  return 55
 elif roadType == 'I':
  return 70
 elif roadType == ...
  return ...

then:
Speed_Limit =
speed(!road_type!)
0 Kudos
SudeshnaGhosh
Emerging Contributor
Hi I did run this, it is as follows:
CODE:

def speed(RouteType):
   if RouteType = 'I', 'PKWY':
        return 70
   elif RouteType = 'US', 'KY':
        return 55
   elif RouteType = 'CNTY':
       return 45
   elif RouteType = 'CITY', 'OTHR':
       return 35
   elif RouteType = 'PRIV':
       return 25

THEN Speed_Limit =

speed (!ROUTE_TYPE!)


However, I am getting an error "ERROR 00989: Python syntax error: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 2)"
Is there any mistake?

Thanks
Sudeshna
0 Kudos
LeonardWilliamson
Occasional Contributor
You need 2 equal signs instead of just one when checking the attribute

def speed(RouteType):
if RouteType == 'I', 'PKWY':
return 70
elif RouteType == 'US', 'KY':
return 55
elif RouteType == 'CNTY':
return 45
elif RouteType == 'CITY', 'OTHR':
return 35
elif RouteType == 'PRIV':
return 25
0 Kudos
MarcinGasior
Frequent Contributor
@LeoNard,
I'm afraid your code also won't work.
To test two or more parameters the syntax is:
if RouteType == 'I' or RouteType == 'PKWY':
    return 70
    ...

#alternatively:

if RouteType in ('I', 'PKWY'):
    return 70
    ...
0 Kudos