14
votes
Accepted
Writing Python code block in field calculator?
I have found that sometimes I need to include a single return rather than in each if/elif/else (I do not know why this is the case, but it has happened enough to now be my go to). Try setting a ...
11
votes
Accepted
Converting full road names to abbreviation using ArcGIS field calculator?
Use a dictionary and if/else with list comprehension to replace the words:
Pre-logic:
def replacename( names, new_names):
d = {'Lane':'Ln', 'Road':'Rd', 'Street':'St'}
return ' '.join([d[...
10
votes
Accepted
Referring to Null value with ArcPy Expressions?
Your problem is that in Python 2 None is less than everything.
>>> None < 20
True
>>> None < numpy.nan
True
>>> None < float('-inf')
True
>>> None < '...
10
votes
Accepted
Field Calculator function caused all my rows to become Null
If you're calculating into the same column that you're getting values from, you always run the risk of losing/messing up the values in that column.
In your case, you'd already calculated the column, ...
10
votes
Accepted
ArcMap Field Calculator Python Parser gives ERROR 999999
Assuming your K field is a float or double type, you are returning a text string "NULL" from your else: clause and you can't store a string in either type. As @bixb0012 notes, if you want ...
9
votes
Using degree symbol in Label Expressions of ArcMap?
Try this:
[lat] + u'\u00B0' + ', ' + [long] + u'\u00B0'
9
votes
Accepted
Field calculator in ArcGIS 10.4 multiline python code
In the field calculator, your parser should be set to Python. With "Show Codeblock" checked, your function definition should go in the "Pre-Logic Script Code" and the expression box at the bottom ...
8
votes
Accepted
8
votes
Reclassifying Vector Field using python parser in field calculator
When calling your function, put the fields between exclamation points
Reclass(!Map Value!)
In your function, use a single word for your variable name
def Reclass(Map_Value):
8
votes
Reducing number of if statements in Field Calculator code block of ArcGIS Pro
You can use in to check for membership:
if FIELD in veg:
return 'Vegetable'
elif FIELD in fruit:
return 'Fruit'
For bonus points use sets, which offer better performance for this:
veg = {'...
7
votes
Accepted
Field Calculator: Split address into house number, street name
This python script seems to do the trick. It allows the user to choose whether to return the house number or the street name to the field (by commenting-out the non-applicable ReturnType line).
def ...
7
votes
Accepted
Calculating the log(10) of a field in ArcMap using the field calculator
In the field calculator, set your parser to python.
Then you can use the math.log10 function.
Here, I'm updating a field called Log10Num with the log10 of a field called Number.
7
votes
Accepted
How to work around Field Calculator's character limit?
Instead of using the Field Calculator, consider writing a Python script that uses an Update Cursor from the arcpy.da module. That way you can theoretically run any length of code you like on each row, ...
7
votes
Accepted
Calculating Polsby-Popper Score Using ArcGIS Pro Field Calculator gives ERROR 000539?
As was mentioned in the comments, your formula should read:
(12.56637 * !AREA_GEO!) / (!PERIM_GEO! ** 2)
or
(12.56637 * !AREA_GEO!) / (!PERIM_GEO! * !PERIM_GEO!)
This page on python operators shows ...
7
votes
Accepted
Writing If-Then statement in ArcGIS Pro field calculator using Python?
If I understand your question to simply be how to migrate the VB code into Python, see below. It's all basically the same, just minor differences in the syntax. The biggest syntax difference is the!...
7
votes
Concatenating address fields with possible blank or null values in fields using Python parser?
def ConcatAddr(Adress,Streetname,Apartment,Citycode,Zipcode):
return ' '.join([str(i) for i in [Adress,Streetname,Apartment,Citycode,Zipcode] if i not in(None,' ')])
Use Python parser and call ...
7
votes
Copying/replacing first letter in column of attribute table in ArcMap with field calculator and Python Parser?
You don't even have to write any code!
Simply edit the table and do a find and replace on the selected field.
Example of replacing B with XXX.
Result of replacement
7
votes
Accepted
Using Python Parser in ArcGIS Pro Field Calculator?
First glance - it looks like your parentheses in the wrong place:
"{}:{}:{}:{}:{}:{}".format("Building ",!Building_Number!," , ",!Street_Type!, " " ,!Street_Number!)
7
votes
Expression in arcpy.CalculateField_management
You have made a basic mistake, you have embed the variable you called field directly within a text expression, i.e. within the "", so it sees it as some text not as a variable.
Do something ...
7
votes
Creating Unique IDs of points in order from west to east
First, to be sure POINT_X has no duplicate values, run next two lines in Python console.
# change layer_name
x_values = [row[0] for row in arcpy.da.SearchCursor("layer_name", "POINT_X&...
6
votes
Field calculator expression
the code block should not contain field name but only variable names.
change !Index! into Index in your code block, but call your function with the field name
define function in code block (any ...
6
votes
Accepted
Complicated expression for Calculate Field for arcpy
You should change "Log" to "math.log" .
If you want to use your code in Field Calculator, just use formula in it:
0.3-.001*math.log(!Field1!+1)-0.02*math.log(!Field2!+1)-0.04*math.log(!Field3!+1)
...
6
votes
Accepted
Python syntax error with If/elif statement in field calculator when using concatenation
You define the function as ifFields but call it with ifField. They need to be the same.
Also I dont think it will return what you want.
Try changing:
"!LandmarkName! + ' ' + '('+ !ZipName! + ')'"
...
6
votes
Accepted
Calculating sequential values in ArcGIS Desktop?
The Esri supplied code starts from 0 and increases by 1. Your method will do the same only if your FID starts at 0 and increases sequentially. An ObjectID or FID is system managed, so if you delete ...
6
votes
Accepted
How to remove first two and last two characters from each label in ArcMap
This can be done using the python parser and a simple bit of string slicing.
In the label expression dialog ensure Advanced is ticked on and the parser is set to python as shown below:
The code in ...
6
votes
Accepted
If-then-else Statements and string errors?
You are only passing one variable into your function: PropType. But on the first line of your function, you refer to a variable named PropClass. Your function has no idea what this variable is, ...
6
votes
Getting angle of point generated from DEM in Raster using Field Calculator in ArcGIS Desktop?
I know ArcGIS's python interpreter is weird, but I'm pretty sure your syntax is wrong. My recollection is that you don't need to use their weird escape characters within the scope of the function ...
6
votes
Accepted
Calculate Sequential Values based on Boolean field
A simple python script can generate the sequential numbering you require using a Calculate Field tool, I show this below. It works because I declare the variables id and previous as global, so their ...
6
votes
Accepted
Field Calculator for Multiple Fields
Your indentation is incorrect,
= should be ==,
=> should be >=
RS10 etc. needs to be enclosed in quotes 'RS10'.
You can shorten the code by using elif and one else at the end.
Try this:
def ...
5
votes
Calculating sequential numbers into sorted table using ArcMap
I had the same question but for a simpler problem, based on having only one Field to sort. I was successful with the following script:
# Pre-Logic Script Code:
# Specify that the target Map Document ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
python-parser × 688field-calculator × 567
arcgis-desktop × 558
arcmap × 173
labeling × 91
arcpy × 77
arcgis-pro × 73
arcgis-10.2 × 50
modelbuilder × 46
arcgis-10.3 × 40
error-000539 × 39
arcgis-10.0 × 28
if-else × 25
arcgis-10.1 × 24
vbscript × 23
attribute-table × 22
expression × 19
error-000989 × 17
string × 16
date × 15
arcgis-10.4 × 14
arcgis-10.5 × 11
error-999999 × 11
null × 11
datetime × 8