0

I have two fields, one is old, Ftype, one is new, Ftype_name. I would like to have python read the numbers in Ftype and populate Ftype_name with the string. I used to know how to do this before they ruined ArcMap. I cant figure that back out.

A work around I was trying is cloning the Ftype into Ftype_name then use the .replace() to replace 390 with the string "LakePond". I keep getting an error that makes no sense to me; "invalid field", the new field is a text field.

I've tried !Ftype_name.replace(390,"LakePond")! and !Ftype_name.replace("390","LakePond")! as the number should have been converted to a string when it was brought over from the Ftype field, which is a long type. The image below is the field calculator, the error, and attributes table for the shapefile.

enter image description here

3
  • 3
    The ! symbols go around the field name, not the entire expression. i.e. !Ftype_name!.replace(390,"LakePond"). If you want to replace all of the values at once, though, use a dictionary: gis.stackexchange.com/a/239644/64255 Commented Feb 28, 2024 at 23:53
  • 2
    !Ftype_name!.replace(390,"LakePond") is not working. This str( !Ftype_name! ).replace('390',"LakePond") does. Commented Feb 29, 2024 at 1:24
  • @mikewatt I've tried that too, I get worse results. In the error it doesn't even list the field name, it always lists a number from that column. The error message i get looks like this "390.replace(390,"LakePond")" I used to code like the way you're suggesting. Doing it like this was the only way i could preserve the field name. Commented Feb 29, 2024 at 16:28

1 Answer 1

-2

Thank you FelixIP. This syntax makes it work.

    str( !Ftype_name! ).replace('390',"LakePond")

I don't understand why the above code works. The numbers are already coded as strings. When I replicated the column, the new column is made of strings. When I interact with the table in Pyscriptor, I used the type() function on rows of the new column, Pyscriptor returns 'str' indicating that the new values are strings. When the type() function is used on the original column 'int' is returned indicating that the original column is filled with integers.

It makes little sense to me why I have to convert the replicated column into strings again. If anyone else knows python syntax to translate the number codes into strings and populate a new column with those strings, without having to replicate the original column first, or why the values need to be converted again; that would be greatly appreciated.

5
  • What I see is explicitly casting a field to a specific Python data type, I don't see any columns getting "cloned." Commented Feb 29, 2024 at 16:52
  • @bixb0012, no there isn't any cloning going on in the code. I had to clone the number values into the new column, and am transforming the numbers into strings one by one. Commented Feb 29, 2024 at 16:56
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Commented Feb 29, 2024 at 17:53
  • No field cloned. It worked on integer Commented Mar 1, 2024 at 8:29
  • Please indicate in comments the reason for down votes. Is my use of the word "clone" confusing to non-english speakers? Should I use "replicate" or "duplicate" instead? Everyone seems to think that the code is replicating the values into the new column. I am replicating the column to preserve the original column and then preforming operations on the new column to convert the number codes into strings. Commented Mar 15, 2024 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.