Hot answers tagged

8

If you want to use the Calculate Field tool (instead of an Update Cursor), what you're assigning to val needs to be an unevaluated python expression. That is, it needs to be identical to the string you would type into the Calculate Field tool if you were using the GUI version. What you're currently assigning to val is "!FEDIRP! !FENAME! !FETYPE!". Any of ...


6

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 = calc_speed(!MTFCC!) You have to evaluate the value of MTFCC field and then make decision based on that. Imho a more elegant solution is to use a Python ...


4

Use the string indices. Instead of creating a whole function, just do something like: !ZIP![:5] --> basically grab everything from 0 index up to but not including the 5th index. So it only grabs the indices 0,1,2,3,4. Check out this tutorial for more information. If there are more than 5 it will just grab the first 5. If there are 5 exact, it will grab ...


4

You want to pass your field name f.name as the name of the field, and then add that to your expression as well. In the field calculator you need to surround your field name with exclamation marks e.g. !myfield! I think something like this should work: import arcpy shp = r"m\team.shp" fields = arcpy.ListFields(shp, "*qty") for f in fields: print f.name ...


3

Expanding on my previous comment... The problem with your statement is that the returned value from string.split is a list and the field you're trying to calculate into is a string, these types are incompatible so the parser ignores the value and sets the string to null. To turn a list into a string you need to use join: >>> a = "Johnny be good" ...


3

You have passed wrong parameter to the NumValue function. It should be: NumValue(!PCT_B100P!)


3

You can look up domain values from the geodatabase and query the fields to find the names of the domain they reference. By limiting this lookup to just the field you're interested in (ST_DESIG) you can pull out all the coded domain values into a dictionary. Once you have this dictionary you can make your arcpy.CalulateField_management code block ...


3

If I'm wrong, I'll give an upvote to whoever corrects me, but I don't think you can do that. Coded-value domains aren't usually very long so here's what I suggest. Since you are using Python, create a dictionary for your coded-value domain. dict = {'0': 'None', '1': 'Divided Major Highway yada yada', '2': 'Major Highway - Class II'}; Then your if ...


3

The first thing that I would check is whether your feature class has a field called FULLNAME. The error message that you are receiving suggests that it does not.


2

There is a Toolbox Scripting tool called Add Geometry Attributes that you can use to create and populate a new attribute with areas and lengths.


2

The solution turned out to be making sure you have the table as a table view, and adding the name of the layer in front of the field name. These are shown in the example here: How to Calculate Field in joined table using ArcPy?


2

Add your new short integer field to your attribute table, right click the field name, and click "Field Calculator". At the top of the Field Calculator window, in the 'Parser' box, check the Python box/bubble. Below the 'Fields' list, check the "Show Codeblock" and enter the following code in the codeblock: def Classify(a): if a == "summer": return 1 ...


2

If the field is numeric, Python expression: !zip![:4] will fail. It is easier to use vb script option: left([zip],5) This will populate output field of numeric and text type.


2

You don't necessarily have to define a function for this one. Try this: str(!PlaceName!).replace(' ', '') The "not defined" error is related to the lack of quotes around the NoSpace1. This could be from the % on either side of the Value. Your expression should be ReplaceName(!Placename!)


2

Reading through your steps it sounds correct to me, although I would question Was your original dataset in meters so that your output from your first step, which you say is in meters, is actually in the same units. If the units are meters, then I think it's safe to assume the area will be in square meters. Do your census tracts match your areas so that ...


2

You can try something like this (see also @radouxju's comment): import arcpy pt = r"C:\AIS\Density_Grid_test.gdb\PivotTable" flist = arcpy.ListFields(pt, "*ShipTypeGroup*") for f in flist: arcpy.AddField_management(pt, f.name.replace("ShipTypeGroup","Ave_STG"),"DOUBLE") newflist = arcpy.ListFields(pt, "*Ave_STG*") for nf in newflist: ...


2

Why not just do the following instead of doing it under a nested loop: import arcpy pt = r"C:\AIS\Density_Grid_test.gdb\PivotTable" arcpy.AddField_management(pt, "Ave_STG", "DOUBLE") arcpy.CalculateField_management(pt, "Ave_STG", "!ShipTypeGroup! / 12", "PYTHON_9.3") or you could try doing it with an UpdateCursor import arcpy pt = ...


1

If you select "table_1" for the Update existing field option, try using this expression: if( "table_2" = 'test', 'y', "table_1") Basically, you're using an IF THEN ELSE statement.


1

Esri's Attribute Assistant has a tool called Generate ID which will automatically generate a new ID whenever a new feature is created. These IDs can be created per feature class to generate different IDs for different feature classes, and can be created based on a specific template if required.


1

You do not need the field calculator for replace at all. You can just use the Find & Replace tool which is accessed from the attribute-table which has the search/replace function the same way as in Excel, for example. It also works for all columns simultaneously or can be set to search for whole values etc.


1

def replaceZeroVal(input, replaceWith): if input <1: return replaceWith else: return input replaceZeroVal(!Bot_Lat!, !Surf_Lat!) Okay, the issue is in the bottom needs to have the exclamation points to say it is calling data specifically from the data table. I found this through GeoNet.


1

You have to use the "Show Codeblock" above where you have your function. Put the function in the codeblock, then call on it in the window where it is now. Also, you have to be careful about the indentation to make sure it is consistent; use 4 spaces at a time for indenting. It looks like you're trying to replace the value if the borehole value is 0... If ...


1

I figured it out, I just used Calculate XY coordinates into my model and this worked.


1

I'm assuming that instead of the second "River" value (under desired ID) you mean "Hill". In the Field Calculator window (for your new field), select the Python parser (in the parser box in the top left of the window), and enter the following into the expression: {"Street": 01, "River": 01, "Mountain": 02, "Hill":02}[!names!] What this does is create a ...


1

Your def addfields section has quite a few indenting errors. Try cleaning that up and see if that fixes it. def addfields(): if arcpy.Exists(Section): fieldlist = [str(f.name) for f in arcpy.ListFields(Section)] if 'TownshipNum' not in fieldlist: arcpy.AddField_management(Section, "TownshipNum", "TEXT", "", "", "5", ...


1

You can do this with a string with the following VBA in the field calculator: Left([FIELD_NAME], InStr(1,[FIELD_NAME], ".") - 1) That will remove the decimal and everything after. If you want some number of digits after the decimal, replace the -1 with + like so: Left([FIELD_NAME], InStr(1,[FIELD_NAME], ".") + 2) will give you 2 decimal places. ...


1

If you're trying to get rid of the decimal and everything to the right of it, you should be able to do something like this: Left([field],InStr([field],".")-1)


1

Create a new field that is the same data type as your original shape lengths field (probably a double). When creating the new field, make sure it has the following parameters: A 'Precision' that is something high, like 20. Precision is the maximum number of digits allowed. So essentially you want to make sure the max is above the total number of digits ...


1

The argument of the split function is expecting a string value surrounded by "" or ''. You have three double quotes. Try surrounding the string with single quotes: !other_tags!.split('"int_name"=>"')[1] To remove the last bit of text, run this statement after running the one noted above: !other_tags!.split(',')[0].replace('"','')



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