Using Field Calculator to add a character in a field in ArcPro

3386
5
Jump to solution
08-22-2019 11:54 AM
ZachSvoboda
New Contributor II

I'm attempting to add a character to an ID field in ArcPro (eg. 2.14 -> 2.014, 2.15 -> 2.015, etc.). Rather than manually do the couple hundred attributes, I'm sure there is a way to do this with a code block in Field Calculator but I can't seem to figure it out. Does anyone have any tips? Thank you!

0 Kudos
1 Solution

Accepted Solutions
rachelg_esri
Esri Contributor

Hello Zach,

In theory, you could also just do

!NumberBefore!.replace(".",".0")

This should just replace the period with a period and a zero. You should only have to put that in the top section and not need a Code Block, I think.

Best regards,

Rachel

Esri Support Services

Rachel Guttmacher
ArcGIS Online Technology Lead
Esri Support Services

View solution in original post

5 Replies
rachelg_esri
Esri Contributor

Hello Zach,

Is there a pattern to where you want to place the character? In other words, is it always going after the second character (in the above example the 2 and the period are the first two characters), or is it always going to be after the decimal, etc?

There should be a way to split the string, concatenate it with the desired character, and then re-concatenate the string halves. It'll only work if there's a pattern though.

Thanks!

Rachel

Esri Support Services

Rachel Guttmacher
ArcGIS Online Technology Lead
Esri Support Services
0 Kudos
ZachSvoboda
New Contributor II

Yes, it will always be after the decimal. Basically it's a double ID with the number before the decimal being the group ID and after the decimal being the attribute ID and I'm just trying to add a hundreds place to the attribute ID. 

I've done similar things in Desktop but I'm not very experienced with Pro (or python code blocks) and believe I'm just missing some element or having something in the wrong spot.

Thanks for the response!

0 Kudos
rachelg_esri
Esri Contributor

Hello Zach,

Assuming:

  • The input values are in a Text field
  • There's only one period
  • You're always wanting to add a zero after the period

Then the following should work for you in Field Calculator

NewField=

addzero(!NumberBefore!)

Code Block=

def addzero(inputString):
   beforePeriod = inputString.split(".")[0]
   afterPeriod = inputString.split(".")[1]
   concatenateAll = beforePeriod+".0"+afterPeriod
   return concatenateAll

Here's a screenshot of the whole thing:

Let me know if that works?

Best regards,

Rachel

Esri Support Services

Rachel Guttmacher
ArcGIS Online Technology Lead
Esri Support Services
rachelg_esri
Esri Contributor

Hello Zach,

In theory, you could also just do

!NumberBefore!.replace(".",".0")

This should just replace the period with a period and a zero. You should only have to put that in the top section and not need a Code Block, I think.

Best regards,

Rachel

Esri Support Services

Rachel Guttmacher
ArcGIS Online Technology Lead
Esri Support Services
ZachSvoboda
New Contributor II

That worked!

Thank you Rachel