# Tag Info

### 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:
• 29.7k
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=...
• 23k
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 !...
• 23k
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 ...
• 29.7k

### 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 ...
• 2,356
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"....
• 65.9k
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[...
• 72.8k
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 < '...
• 65.9k
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, ...
• 65.9k
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 ...
• 65.9k
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 ...
• 29.7k

### Using degree symbol in Label Expressions of ArcMap?

Try this: [lat] + u'\u00B0' + ', ' + [long] + u'\u00B0'
• 6,916
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 ...
• 979

### 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 ...
• 4,128

### 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 ...
• 4,928
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 =...
• 29.9k
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))
• 29.7k
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 &...
• 325
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(): ...
• 1,200

### 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.
• 40.9k

### 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 ...
• 29.7k
Accepted

### Field Calculator: Round UP to the nearest thousand

You can use this: -(-!cost!//1000)*1000
• 1,239

### 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):
• 41.9k

### 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 = {'...
• 5,083
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, ...
• 8,286
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,...
• 8,565

### 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 ...
• 3,496
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, ...
• 29.7k

### 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 ' ...
• 29.9k