# Tag Info

41

I think this is what you want .... using python you could do the following (assumes YEAR and COUNT0X fields are integers) Change to Python as highlighted below Add the code to the 'Pre Logic' input box Change the yearVal as needed for each field you calculate (Count01 would use 2001, Count02 would use 2002, etc) Note Python using indentation to parse ...

16

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:

15

You slice the string to be the third character through the end of the string (strings are zero indexed). !Name![2:]

14

"Solution" with 2 sorted fields (ascending): mxd = arcpy.mapping.MapDocument("CURRENT") lr=arcpy.mapping.ListLayers(mxd) tbl=arcpy.da.TableToNumPyArray(lr,("oid","A","B")) bs=sorted(tbl, key=lambda x: (x, x)) def sortSeq(fid,a,b): for i,ent in enumerate(bs): if ent==fid: return i -------------------------------------- sortSeq( !OID!, !A!, !...

14

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 variable and returning just that variable at the end. Codeblock: def SpeedCalc(MTFCC): x = 9999 if MTFCC == "S1400": x = 25 elif MTFCC == "...

13

I think the problem lies with your statement: def !gridcode! (value): is incorrect, see http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000 def something(value): It's hard to read what you've got there but I think this is it: Full expression: def TextValue(FromCode): if FromCode > 0 and FromCode < 50: return "...

12

you need to use this code within ArcMap and the field calculator. Add your feature class in the table of content, right click on it to open the table, right click on the name of the field and launch the field calculator. Then you check for codeblock and copy the code you mentioned. now for your code snippets, here is what I would do rec=0 def ...

12

What you are looking at is an Advanced Field Calculation. It's a little confusing because you're kind of referencing it backwards. If you right-click on a field in an attribute table and select the field calculator, you'll notice an option in the field calculator window to change the parser to Python as well as a check box named 'Show Code Block'. When ...

12

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 !locality! to relevant field. UPDATE: This variation of expression: d={} def GroupOrder(groupID): N=d.get(groupID,0);N+=1 d[groupID]=N return N Should work ...

11

I found a similar question on an ESRI forum that had a good answer. The image below shows a sample polyline dataset to test the field calculator on. The attached code block below calculates the degrees from the x-axis. Try the following steps: Use the Split Line at Vertices tool to create individual line segments from your poly line. Add a field in your ...

11

You have to include quotes within your expression string. Try this: fcList = arcpy.ListFeatureClasses() fieldName1 = "Author" expression1 = "\"John Doe\"" for fc in fcList: arcpy.AddField_management(fc,fieldName1, "TEXT") arcpy.CalculateField_management(fc, fieldName1, expression1) By adding in escaped double ...

11

I do not think you will find a way to do this because there is an existing, not yet Under Consideration, ArcGIS Idea to have the 'Field Calculator default to Python'.

10

Here is a code block for the Field Calculator that will do what you require. Open the attribute table Select all of the records that contain a NULL value in the desired field Open up the Field Calculator and insert this code in the appropriate sections. Accumulative and sequential calculations Calculate a sequential ID or number based on an interval. ...

10

All of the trigonometric functions you need are in the math module. I presume you'll want atan2() which is the equivalent of atan(y/x). For the mod function, you'll need to use the percent symbol. Each function also has a simplified version (a) since you are calculating some constants. They're identical functions but will be faster. Parser: Python ...

10

Solution with re module You can use the built-in re module in Python for that. import re sr = re.split(' - |/',s) Field calculator view (where FeatureStr is the source field with 264K - Name of Place/FeatureType and Split is the output field with : Name of Place. Pre-Logic Script Code: def splitText(field): import re return re.split(' - |/', field)[...

10

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".minimum) * 100 This will work as long as you have already calculated statistics for the raster, otherwise it will fail as "raster".minimum will return None. ...

10

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[word] if word in d else word for word in names.split()]) Call with: replacename( !names!, !new_names!)

10

Your problem is that in Python 2 None is less than everything. >>> None < 20 True >>> None < numpy.nan True >>> None < float('-inf') True >>> None < 'Everything' True Notes: In ArcGIS Pro (Python 3) you'll get a TypeError using a numeric comparison operator with None which will help avoid this issue. This ...

9

I think this is just a bug/limitation of the Python parser with the field calculator/Calculate Field tool. If a newline is encountered within the text field, a SyntaxError: EOL while scanning string literal occurs no matter what you try. I can reproduce the issue at 10.1 SP1 by importing the following CSV file into a file geodatabase, adding a field and ...

9

I can see from your field name !DEAD_VOL_PER_HA_SPP1_125! that the field you are calculating is likely a float or integer type field. Assuming this is true, you cannot write a space or '' into a number-based field (e.g. return ''). Valid values for number-based fields include None or a numeric value. You will need to either create a new text field to ...

9

No need to write a function here. Just hit the Python radio button, and then write this into the box: Address: + " " + str(!Address_No!) + " " + !StreetName! + " " + !Street_Ty! What is important: - field names are represented by their name but embraced by exclamation points: ! - any numeric field needs to be converted to a string when concatenating with ...

9

you need to pass the fields you're using in your codeblock as arguments to the function, rather than the field that you're calculating. So your code might look something like this: def cal(House_Num, FROM_ADDR, TO_ADDR): if ((House_Num >= FROM_ADDR) and (House_Num <= TO_ADDR)) : return 88 else: return 1 Expression: cal(!...

9

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 from the geoprocessing results. Run the Field Calculator Open the Results pane (Geoprocessing > Results) Find the corresponding tool results, right-click and ...

9

Try this: [lat] + u'\u00B0' + ', ' + [long] + u'\u00B0'

9

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 should be populated with: u_biotype(!BIOT1!) Your code block should end with the return function as well. The "Calculate Ranges" example on the ArcGIS help page ...

8

Eric’s Complete Guide To VT_DATE is a good guide if you are asking what I think you are asking. From here, you can get the string you want fairly easily in Python using datetime: import datetime new_dt = datetime.datetime(1899, 12, 30) + datetime.timedelta(days=41256.260799) formatted_string = new_dt.time().strftime("%H:%S") That should be pretty easy to ...

8

>>>'R12345678910'[1:] '12345678910' Appending [1:] to a string in python will remove the initial character. Look up slice in the python help. To use this in the ArcGIS field calculator, you will need to turn on Python parsing for the unitCode = enter !your_field_name![1:]

8

Using Python, the calculation would look like this: For the Pre-Logic Script: def Calc(a,b,c): if a == "000": return b else: return c For the expression: Calc( !A! , !B! , !C! )

8

This answer is essentially the same as listed above, however it's a way to not have to use the code block... for the Count01 field, you would set the parser to Python and then set your calculation to 1 if !YEAR! == 2001 else 0 The way this reads is: Set the field to 1 if the YEAR field is 2001, if it's not 2001, then set it to 0... If you have ...

8

I would make two changes: Make sure your indentation is correct Use len() to count the characters. Make sure you are writing to a text field. def reclass(f1): if len(f1) >= 4: return "Yes" else: return "No" Label = reclass( !HUB! )

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