I have a tracts field (Tract) which I want to strip off the first 4 numbers in an attribute table. I created a new field (Tract2) for it. I've tried several commands without success using the field calculator. I'm receiving errors
Here the data that I'm trying to join to Census Tract file.
https://oehha.ca.gov/calenviroscreen/report/calenviroscreen-40 (I downloaded the shape file)
The reason I'm doing it as the CES tract boundaries are more general and when overlaying Tiger line state/counties there's not a perfect alignment of boundaries like there should be.
I did manage to get the data in string format (6 characters in length). When clicking on validate join it says that everything is good. So I click to join the tables. When I open the Census Tract data I get an error when opening the attribute table of Invalidate Sql Statement Was Used.
Tract codes are digits, but they must be text to do a join to another table that has a tract text field. To create a text field Add a field "tract1" as type text, and populate it with Calculate Field (python syntax: str(int(!TRACT!) to a) make it integer and b) convert to text)
Getting data to load as text from a csv is tricky, the usual method is to add a row of dummy data to force type, for example put "aaaaaa" in the place of the tract column. The csv type issue is similar to issues loading data from excel https://pro.arcgis.com/en/pro-app/latest/help/data/excel/work-with-excel-in-arcgis-pro.htm
I think we have helped you as much as you can until you have data in text.
The original data source is numerical (double) so when I create a text field it puts .0 at the end. The data is a shape file. The .0 prevents me from joining it with the Census Tract data I downloaded. So, how can drop the .0 in the newly created text Tract Field from Python?
The following work except it puts a .0 in at the end. I need it without the .0 so I can join it with another table.
If your data are numbers, this Python expression should work without a code block. Assuming Tract2 is a text field, which it should be.
str(!Tract!)[4:]
1st 4 number will be removed
D'oh! Thank you for catching that. Although, thinking about it more, it's not totally clear if "strip off" means that the first 4 numbers are being removed from the output, or if it's the first 4 that they want to keep. I guess [4:] would be the other way.
Census tract codes are text fields, so your syntax would work.
However, your code gets the last four numbers. To get the first 4 numbers you can use the Left function, or in python use the slice expression [:4].
What have you tried so far? And what errors do you get? What kind of field is it, string or numeric?
Assuming a string, you could try either of these approaches.
Arcade:
Right($feature.Tract, Count($feature.Tract) - 4)
Python:
!Tract![4:]
Edit: Tweaked the Python bit. Thanks, Curt! Also, could @jfkuhn please elaborate on the intended output here? Do you want the first four characters to end up in the new field, or do you want the remaining characters after the first 4 are removed?
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.