I'm new to python. What tool would I use to --
so that I may use the calculate tool to establish a new field (TRACT) and populate 704202 into the new (TEXT) field I create for calculation please? I want to be like all the popular analysts online ; -)
Are all of them following the same format? GEO_ID + space + 11 digits?
If so:
intext = 'GEO_ID 25027704202'
intext.split(' ')[-1][-6:]
'704202'
R_
Hi EricG,
It depends on the context. I think what you are saying is that you have a dataset that has a geoid field and want to do this:
-- Add a TRACT field to the dataset.
-- Set the tract field to the last 6 characters of the geoid.
Again without more context, it's difficult to guide you on the details, but assuming you're talking about a field in a dataset that is iterable with a cursor, you could do something like this:
import arcpy
arcpy.env.workspace = "C:/mydatapath.gdb" # modify to reflect your workspace
theData = "/mydata" # modify to dataset with a geo_id column
theGEOIDfield = "GEOID" # modify to geo id column name
arcpy.management.AddField(theData, "TRACT", "TEXT")
with arcpy.da.UpdateCursor(theData, [theGEOIDfield, "TRACT"]) as uc:
for row in uc:
row[1] = row[0][-6:] # Set TRACT to the last 6 digits of the GEOID.
uc.updateRow(row) # Commit the change.
del uc
I've placed a pic to gain more insight. @RhettZufelt, I've included you as well to respond to both of you.
I have the GEO_ID, and would like to keep the GEO_ID intact, but, would like to take the last digits associated with the TRACT and duplicate it into another field called TRACT. Is this helpful?
Guess I overthough that one a bit. No need for the split if you are always grabbing the last 6 digits:
Calc tool, Expression Type Python 3
!GEOIDFIELD![-6:]
R_
I had a feeling it might not be too complicated. Is there a web resource I can learn more from about these things?
!GEOIDFIELD![-6:]
This is refered to as slicing in python, and is not really deleting values, rather 'grabbing' values from the string.
many ways to do it, and the links I provide gives some good examples, but google has tons of into on it.
Example:
!GEOIDFIELD![:6] would grab the first 6 digits.
!GEOIDFIELD![6:] skip first 6 digits and grab the rest.
........
R_