Skip to main content
19 votes

Generate random floating value

In the field calculator, show Codeblock. In the pre-logic script code box: import random def randnum(): return random.random() In the expression box: randnum() Result in a new float field:
Midavalo's user avatar
  • 29.7k
15 votes
Accepted

Calculating sequential numbers into sorted table using ArcMap

"Solution" with 2 sorted fields (ascending): mxd = arcpy.mapping.MapDocument("CURRENT") lr=arcpy.mapping.ListLayers(mxd)[0] tbl=arcpy.da.TableToNumPyArray(lr,("oid","A","B")) bs=sorted(tbl, key=...
FelixIP's user avatar
  • 23k
14 votes
Accepted

Auto incrementing field based on groups within feature class?

Field calculator for Python d={} def GroupOrder(groupID): if groupID in d: d[groupID]+=1 else: d[groupID]=1 return d[groupID] --------------------------- GroupOrder( !locality! ) Change !...
FelixIP's user avatar
  • 23k
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 ...
Midavalo's user avatar
  • 29.7k
11 votes

Calculating incidence angle of polyline using ArcMap

Additional to Aarons correct answer here the a way to use atan2 as suggested by Mike T and the transformation to geographical Degrees where the geographical North is at 0/360°. # Pre-Loci Script Code ...
wittich's user avatar
  • 2,356
11 votes
Accepted

Normalizing raster using raster calculator in ArcGIS 10.3.1?

Raster objects have minimum and maximum (as well as mean and standardDeviation) properties that can be accessed in the raster calculator. ("raster" - "raster".minimum) / ("raster".maximum - "raster"....
user2856's user avatar
  • 65.9k
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[...
BERA's user avatar
  • 72.8k
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 < '...
user2856's user avatar
  • 65.9k
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, ...
user2856's user avatar
  • 65.9k
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 ...
user2856's user avatar
  • 65.9k
9 votes
Accepted

Calculating field with hour from date time field with ArcPy?

The easiest way to find out how to reproduce something in python is to run it once in ArcMap using the ArcToolbox tool (or in this case, the Field Calculator), and then choosing Copy Python Snippet ...
Midavalo's user avatar
  • 29.7k
9 votes

Using degree symbol in Label Expressions of ArcMap?

Try this: [lat] + u'\u00B0' + ', ' + [long] + u'\u00B0'
Maksim's user avatar
  • 6,916
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 ...
Lovette's user avatar
  • 979
8 votes

Concatenating two fields (type double) into single field?

Your output field should be a text field. If you set the field calculator's parser to Python, you could enter this for the formula: '{}.{}'.format( !TractField! , !BlockField! ) If they really are ...
Tom's user avatar
  • 4,128
8 votes

ArcMap sometimes ignores labeling expression

This is because you try to use > as a character in your label, as well as for formatting/programming your label. Nothing wrong with that, it's how it works. But if you want to use those characters ...
Martin's user avatar
  • 4,928
8 votes
Accepted

Using ArcGIS Field Calculator to populate number field from text field?

You are almost there. What you are trying to do is: Codeblock: def calc_speed(field_value): if field_value == 'S1100': return 65 if field_value == 'S1200': return 45 Speed field Speed =...
Alex Tereshenkov's user avatar
8 votes
Accepted

Calculate Field using variable in arcpy

Try passing your variable to your string: var_a = 6.2 arcpy.CalculateField_management (in_table, in_field,"[FIELD_A]*100/{}".format(var_a))
Midavalo's user avatar
  • 29.7k
8 votes
Accepted

Problem with direction calculation in ArcGIS

In one of your first lines you are missing a bracket (. This is your code with the bracket added: def Direction(angle): if (angle >= 67.5 and angle <=90): return "N" if (angle &...
JiyuuSensei's user avatar
8 votes
Accepted

Stacked Label using If/Else in a Label Expression

You could try something like the following. def FindLabel ([BEN],[ETH],[TOL],[ISO]): vars = {"B":[BEN],"E":[ETH],"T":[TOL],"I":[ISO]} labelStrings = [] for k,v in vars.items(): ...
PMK's user avatar
  • 1,200
8 votes

Remove last 5 characters with Field Calculator in ArcGIS Desktop?

In the field calculator choose Python Parser and write the following formula: !Field_Name!.replace(" City","") Where: !Field_Name! is the name of the field that contains city names.
ahmadhanb's user avatar
  • 40.9k
8 votes

Python Max Function in Field Calculator for Date values

You need to convert your date into datetime object for max() to work correctly. It appears field calculator doesn't see the value as a datetime object. You may need to adjust the date format to suit ...
Midavalo's user avatar
  • 29.7k
8 votes
Accepted

Field Calculator: Round UP to the nearest thousand

You can use this: -(-!cost!//1000)*1000
atxgis's user avatar
  • 1,239
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):
JGH's user avatar
  • 41.9k
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 = {'...
mikewatt's user avatar
  • 5,083
7 votes
Accepted

Field calculator if and elseif statement with <Null> values in ArcGis 10.1

Here's a simple expression with no need to define a function in the codeblock: Headwater = 0 if !NextDown_Copy! else 1 This checks if each value of NextDown_Copy is truthy (i.e. not a null, 0, ...
nmpeterson's user avatar
  • 8,286
7 votes
Accepted

Field Calculator text field to new shapefile

The error is occurring because you do not quote the value properly. To send a string to the field calculator using a script you have to send a quoted string with double quote characters in the string,...
Richard Fairhurst's user avatar
7 votes

Using Asterisks in Python parser of ArcGIS Label Expression Builder?

Please follow the python syntax-Just check if hike is in the Name. Below should work def FindLabel ([Name]): if 'hike' in [Name]: return [Name] But if you need to label only Name does ...
Learner's user avatar
  • 3,496
7 votes
Accepted

Editing Field using VBscript or Python in Attribute Table?

This will need you to modify values based on your GRIDCODE as I can only see 4 and 16 in your table, but to do this in the Field Calculator is fairly straightforward. In the Field Calculator window, ...
Midavalo's user avatar
  • 29.7k
7 votes

Removing white space from field values?

You are looking for the str.strip() method: Calculate your field to be: !account!.strip() This will remove all the whitespaces both to the left and to the right. Having this: s = ' 83128313 ' ...
Alex Tereshenkov's user avatar
7 votes
Accepted

Label expression in ArcGIS gives TypeError coercing to Unicode need string or buffer NoneType found?

A value in one (or both) of your fields is a number, and python is trying to add it due to the + symbol, or alternatively it could be a value that can't be handled in its current form. Either way you'...
Midavalo's user avatar
  • 29.7k

Only top scored, non community-wiki answers of a minimum length are eligible