37

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 ...


17

I would use regular expressions. If you put the following in the code block section of the calculator it will return a string that has all digits stripped, no matter were they are: import re def strip_digits(s): return re.sub("\d+", "", s) This can then be called from the calculate box as: strip_digits(!column_name!) If you want to replace characters,...


15

I have found that in 10.0 Field Calculator is quite weird. But I've managed to get it work. The main idea is to enclose field name with single quotes. Example. let suppose we have fields text1 and text2. Rather than Calculating field text2 with expression !text1!, which probably will fail, try this one: '!text1'. As you see I am using single quotes here. ...


15

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


14

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:


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

You could include the Python dictionary of state abbreviation:full-name pairs here states = { 'AK': 'Alaska', 'AL': 'Alabama', 'AR': 'Arkansas', # etc... } as the Codeblock in Field Calculator, and then use something like the following as the actual calculation: !City! + ", " + states[!State!] I should add that you could perform this ...


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

When you go to dispay, calculate or label the field you could just use, round ([my_field],2) also to change the field behavior in arcmap...


12

You want the in method detailed here: http://docs.python.org/reference/expressions.html#in Swap if Name=="*Emergent*": with if "Emergent" in Name:


12

For this you can use UpdateCursor, which opens the feature class or table and steps through each record (row) incrementally. The script below works on this test data +-----------------------+ | Time| Home_Away|Trip | +-----|----------|------+ | 1 | 0 | <nul>| | 2 | 1 | <nul>| | 4 | 1 | <nul>| | 5 | 0 | ...


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

"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=lambda x: (x[1], x[2])) def sortSeq(fid,a,b): for i,ent in enumerate(bs): if ent[0]==fid: return i -------------------------------------- sortSeq( !OID!, !A!, !...


11

Have you tried something like what's below in the Field Calculator? round(!FieldName!, 2) Make sure you set the Parser to Python in the Field Calculator.


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'.


11

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 ...


10

What you want to do in the codeblock for Python is define a function, and then call the function using your attributes as parameters as follows: def comparison(left,right): if left < right: return left else: return right output = comparison(!MIN_LEFT!,!MIN_RIGHT!) Then, all you need in the calculation is output, as you already ...


10

Try using Sort (Data Management) followed by Calculate Field (Data Management) using the auto-increment example on the Calculate Field examples help page. If you need to sort the data and update it in-place (no intermediate dataset), then I think you would have to use an UpdateCursor which can also sort by a field.


10

If you are using version 10.1 and you are sure you want to get rid of every instance of double quotes you can use: !testing!.replace("\"","") If anyone knows why this works in 10.1 and not 10.0 I would be interested. Here are the results entry from my run.


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

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 ...


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)[1] 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(' - |/', ...


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

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(!...


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