Select to view content in your preferred language

Need help with python code for ArcMap Field Calculator

363
7
Jump to solution
2 weeks ago
EGardner7
New Contributor II

I am attempting to use python to change values in a field named CableName. I am trying to change the capitalization for the first word/words in the field to only the first letter capitalized. For example PRESTON Avenue 420 to Preston Avenue 420. Or N HIGHLAND Avenue 519 to N Highland Avenue 519. I am trying to just focus on the values that contain the word Avenue so I can exclude any variation and then I will replace Avenue with Street, Cove, Road, etc. I've tried to use copilot and I have had some success with other scripts but this one is giving me some trouble. Here are some scripts that have worked for me in the past: 

!CableName! .replace("St.", "Street")

 

!LegacyName! if !CableName! is None else !CableName! 

 

I am trying to get Copilot to write this in a similar format but here is what it gives me:

def capitalize_first_letter(word):
    # Check if the word is in all caps
    if word.isupper():
        # Capitalize the first letter and make the rest lowercase
        return word.capitalize()
    else:
        return word

# Usage example:
# Call the function with the field value
result = capitalize_first_letter(!CableName!)
result

I am new to using Python but if I could get this to work it would save me months of work. Would really appreciate any help with this.

 

3 Solutions

Accepted Solutions
JeffWard
Regular Contributor II

Have you tried this?

 

!CableName!.title()

 

Jeff Ward
Summit County, Utah

View solution in original post

EGardner7
New Contributor II

Wow I didn't realize I could do it all at once! I was able to update over 24,000 records so I really appreciate your help. Would you possibly know how I could move numbers from the end of a field to the front? For example Stratford Lane 215 to 215 Stratford Lane or Gordon Street 237-R to 237-R Gordon Street.

View solution in original post

0 Kudos
emarcelloni
New Contributor II

Hello @EGardner7 for your last question you can try this code, works fine for your examples. This code move the last part of your address to the front of your address. Move always the last part regardless of whether they are numbers or letters.

' '.join([!YourAddressField!.split()[-1]]+!YourAddressField!.split()[:-1])

- Regards!, Esteban Marcelloni

 

Image 1: I convert Provincia to Departamento

imagen.png

 

View solution in original post

7 Replies
JeffWard
Regular Contributor II

Have you tried this?

 

!CableName!.title()

 

Jeff Ward
Summit County, Utah
EGardner7
New Contributor II

Wow I didn't realize I could do it all at once! I was able to update over 24,000 records so I really appreciate your help. Would you possibly know how I could move numbers from the end of a field to the front? For example Stratford Lane 215 to 215 Stratford Lane or Gordon Street 237-R to 237-R Gordon Street.

0 Kudos
emarcelloni
New Contributor II

Hello @EGardner7 for your last question you can try this code, works fine for your examples. This code move the last part of your address to the front of your address. Move always the last part regardless of whether they are numbers or letters.

' '.join([!YourAddressField!.split()[-1]]+!YourAddressField!.split()[:-1])

- Regards!, Esteban Marcelloni

 

Image 1: I convert Provincia to Departamento

imagen.png

 

EGardner7
New Contributor II

Thank you Esteban! That is amazing. Do you have any tips for learning how to use python? This is all new to me, so I would love to learn what else I could do with it. 

0 Kudos
emarcelloni
New Contributor II

I learnt a lot from here: https://www.kaggle.com/learn 

In the learn section you have shorts and dinamics Courses. This site is for people who wants to learn DataScience and MachineLearning but in the process you will learn a lot of Python. I recommend you to do all the Courses starting from here: https://www.kaggle.com/learn/python

Regards! Esteban Marcelloni

EGardner7
New Contributor II

Thanks Esteban I will definitely check that out! One more thing, I noticed that after I entered that last python script sometimes it would move things other than the numbers. For 90% of them it worked but for some it switched things like Steam Mill Ferry Road 1231 to Road 1231 Steam Mill Ferry Road instead of 1231 Steam Mill Ferry Road. Is there a way I could enter something that would just move the word Road to the end, and I could switch that out after that works with Cove, Circle, Street, etc. 

0 Kudos
JeffWard
Regular Contributor II

I always try things in the Python window before I actually run it in the field calculator.

I got this to work-

' '.join([!FieldName!.split()[-1], ' '.join(!FieldName!.split()[:-1])])
Jeff Ward
Summit County, Utah
0 Kudos