Select to view content in your preferred language

Python Tool Box

1001
7
05-04-2023 12:12 PM
Labels (3)
EricG
by
Occasional Contributor

I'm new to python. What tool would I use to --

  • Take a (TEXT) GEO_ID 25027704202
  • Remove the ST (25) & COUNTY FIPS (027)

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 ; -)

 

Rhode Island Department of Health (RIDOH)
0 Kudos
7 Replies
RhettZufelt
MVP Notable Contributor

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_

 

0 Kudos
LauraTateosian
Regular Contributor

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

 

0 Kudos
EricGurney
Emerging Contributor

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?

0 Kudos
RhettZufelt
MVP Notable Contributor

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_

0 Kudos
EricGurney
Emerging Contributor

I had a feeling it might not be too complicated. Is there a web resource I can learn more from about these things?

0 Kudos
EricGurney
Emerging Contributor
 

@RhettZufelt

!GEOIDFIELD![-6:]
Worked like a charm. So would !GEOIDFIELD! [+6] have deleted from the right side of the number string? I'd really enjoy reading a link of some of the simpler python processes. But, just don't know what's best to review. Thank you and Thank you @LauraTateosian for taking the time out to reply!
 
 
0 Kudos
RhettZufelt
MVP Notable Contributor

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_

 

0 Kudos