Reorder text using Python in the Field Calculator

Blog Post created by timothy_hales-esristaff Employee on Jan 16, 2015

I have found times when I need to reorder the text within a field such as in this example with the name being last name, first name.

This can be a simple expression in the Field Calculator using the string.split() operation:


!Name!.split(", ")[1] + ' ' + !Name!.split(", ")[0]


But what about those cases where there may be two or more words?

In this case we can use an if/else statement to determine the number of words in the field and then rearrange accordingly.



Pre-Logic Script Code

def reorder(name):
 if (len(name.split(" ")) ==2):
  return name.split(" ")[1] + ' ' + name.split(" ")[0]
 elif (len(name.split(" ")) ==3):
  return name.split(" ")[2] + ' ' + name.split(" ")[0] + ' ' + name.split(" ")[1]


Name2 =

reorder( !Name!)


Note: Make sure you set the correct split characters. In the second example it only uses a space and not a comma. Also, the example is only set up for 2 and 3 words. Additional conditional statement can be added to accommodate for you data.