Select to view content in your preferred language

Need help with Python Calculations!

1256
4
04-18-2012 06:24 AM
MichelleCouden1
Deactivated User
Can anyone please help me with a short program in Python scripting. I need a short program that will allow me to do a field calculation (Such as : population per square mile), and put the answer in a field (POPSQMILE) that was created in the editing session. Anything will help. Thanks!!
Tags (2)
0 Kudos
4 Replies
AnthonyTimpson2
Regular Contributor
If you have a population field and a Square miles field you can just run the calculation on the Pop/sq mile field using the field calculator.

The equation will look like  [population field]/[sq miles field] in the field calculator

Yielding the result of people per square mile.

To access the field calculator right click on the field you want to populate and select Field Calculator from the menu. 

No need to dive into Python on this if you dont want to.
0 Kudos
RyanForbes1
Occasional Contributor
That's right - the only situation I can imagine where you would prefer Python over doing the field calculation is if you have many, many features where you need to perform this calculation and you want to automate it.  In that situation you might be interested in reading http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000 as this details how to use the field calculator in arcpy.
0 Kudos
MichelleCouden1
Deactivated User
I know I can use the field calculator. I need a python program to do the same thing. Calculate two fields into one field. I was wondering if anyone knew how to write such a program. Please help!!!
0 Kudos
RyanForbes1
Occasional Contributor
The link I posted has all of the information you need to make the calculation happen via Python.

import arcpy

# path on the drive to the feature you're trying to manipulate.
inFeature = r"..."

# SQL expression you want to do.
expression = "[field1] + [field2]"

# The name of the output field.
outField = "field3"

# the tool.
arcpy.CalculateField_management(inFeature, outField, expression)
0 Kudos