Arcpy – Renaming Multiple Fields Using arcpy.AlterField_management

arcpyfields-attributes

After several attempts and researches (I haven't found examples for renaming several fields directly without using a list), I tried the following code to rename several fields. However, I get the following:

ERROR 000664: Invalid input: The type of dataset is not supported.

arcpy.AlterField_management(in_table = "file.shp",
                       field = ['Old_title','Old_title_1','Old_title_2'],
                       new_field_name = ['New_title','New_title_1','New_title_2'])

Best Answer

Firstly, as mentioned by @orbl_soil, you should be using a feature class (or table) from a geodatabase to use AlterField_management. Secondly, you should make sure that your feature class (or table) is fully qualified, including the full path of the workspace and feature class (or table). Eg, fc = r"C:\path\to\workspace\feature_class"

Then it's a simple one liner using Python list comprehension to modify multiple field names. However, as @Paul alluded to below, this doesn't really offer any benefits over a more explicit/two-line for loop apart from the fact that it is all in one line. It also has the minor drawback of producing an unused list result (in this case, something like [None, None, None, None]) which is a small waste of resources.

But if you would like to pursue this method, here are a few examples, which demonstrate how it can be done for different situations:

Example 1 - Use a dictionary of old/new field names for each field you want to alter:

namesDict = {"a": "a_new", "b": "b_new", "c": "c_new"}

[arcpy.AlterField_management(fc, f, namesDict[f]) for f in namesDict]

Example 2 - Append "_renamed" to all non-required field names:

[arcpy.AlterField_management(fc, f.name, f.name+"_renamed") for f in arcpy.ListFields(fc) if not f.required]

NB: In the example above, it changes all field names that can be changed. Required fields, such as OBJECTID and Shape_Length, cannot have their names altered.

Example 3 - After performing a 'dissolve' or 'summary statistics' the resulting feature class or table will usually have several fields with "FIRST_" or "SUM_" or similar prefixed to them. Sometimes you don't want this, so you can easily remove these prefixes by:

[arcpy.AlterField_management(fc, f.name, f.name.replace("FIRST_", "")) for f in arcpy.ListFields(fc, "FIRST_*")]

Equivalent for Loop Examples - Using a two-line loop, that does not waste resources creating an unused list, and is more 'pythonic' in the sense that clarity is more important than conciseness, the equivalents of the above three examples would be:

Eg 1:

for fieldName in namesDict:
        arcpy.AlterField_management(fc, fieldName, namesDict[fieldName])

Eg 2:

for fieldName in [f.name for f in arcpy.ListFields(fc) if not f.required]:
        arcpy.AlterField_management(fc, fieldName, fieldName+"_renamed")

Eg 3:

for fieldName in [f.name for f in arcpy.ListFields(fc, "FIRST_*")]:
        arcpy.AlterField_management(fc, fieldName, fieldName.replace("FIRST_", ""))
Related Question