HOW TO

Alter field aliases using Python in ArcGIS Pro

Last Published: April 7, 2023

Summary

In ArcGIS Pro, the Fields toolset contains tools to create and modify fields for any geodatabase table or feature class. These modifications can be made by running a Python script in ArcGIS Pro and this article provides the workflow.

The image below shows the Schools_all attribute table with field aliases containing underscores and lower case letters.

The Schools_all attribute table

Procedure

  1. Start ArcGIS Pro and open the project.
  2. To open the Python window, on the top ribbon, click Analysis, click the Python drop-down list arrow and select Python Window.
The Python window icon in the Python drop-down box on the Analysis tab
  1. Specify the following script in the Python window.
    1. Import the system modules.
import arcgis
import os
  1. Specify the ArcPy function to check extensions and overwrite outputs.
arcpy.env.overwriteOutput = True
  1. Specify a variable Syntax to list all the field names of the layer.
<variableName> = arcpy.ListFields('<layerName>')
  1. Specify the ArcPy function to apply the changes to the field alias. In this example, the initial letter is capitalized and '_' is replaced with a space.
print("Changing the field alias to field name by Capitalizing the initial letter")
for field in <variableName>:
    if not field.required:
        arcpy.management.AlterField('<layerName>', field.name, field.name, field.name.rstrip(field.name[-2:]))
        arcpy.management.AlterField('<layerName>', field.name, field.name, field.name.replace("_"," ").title())
        print("Completed")

The following code block demonstrates the full working script.

import arcpy
import os
arcpy.env.overwriteOutput=True
infeature=arcpy.GetParameterAsText(0)
fields=arcpy.ListFields('Schools_all')
print("Changing the field alias to field name by Capitalizing the initial letter")
for field in fields:
    if not field.required:
        arcpy.management.AlterField('Schools_all', field.name, field.name,field.name.rstrip(field.name[-2:]))
        arcpy.management.AlterField('Schools_all', field.name, field.name, field.name.replace("_"," ").title())
        print("Completed")

The image below shows the updated field aliases using Python in ArcGIS Pro.

Part of the Python script and results of the updated field aliases

Article ID: 000029252

Software:
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options