Skip to main content

Table.rename_columns

rename_columnscolumn_mapcase_sensitivityerror_on_missing_columnson_problems

Group: Metadata
Aliases: rename_fields, select_columns, select_fields

Documentation

Returns a new table with the columns renamed based on either a mapping from the old name to the new or a positional list of new names.

Arguments

  • column_map: Mapping from old column names to new or a vector of new column names to apply by position. The mapping can be provided as a Dictionary or a Vector of pairs of old and new names. A Table can also be used either with a single column of new names or two columns with old (first column) and new names (second column).
  • case_sensitivity: Controls whether to be case sensitive when matching column names.
  • error_on_missing_columns: Specifies if a missing input column should result in an error regardless of the on_problems settings. Defaults to True.
  • on_problems: Specifies how to handle problems if they occur, reporting them as warnings by default.

Returns

  • A new table with the columns renamed based on the column_map.

Examples

Rename the "Alpha" column to "Delta"

      table = Table.from_rows ["Alpha", "Beta"] [["John", "25"]]
output = table.rename_columns [Pair.Value "Alpha" "Delta"]

Returns a Table

DeltaBeta
John25

Rename the last column to "LastColumn"

      table = Table.from_rows ["Alpha", "Beta"] [["John", "25"]]
output = table.rename_columns [Pair.Value -1 "LastColumn"]

Returns a Table

AlphaLastColumn
John25

Rename the "Alpha" column to "Beta" and last column to "LastColumn"

      table = Table.from_rows ["Alpha", "Age", "State"] [["John", 25, "NY"], ["Paul", 35, "CA"]]
output = table.rename_columns [Pair.Value "Alpha" "Beta", Pair.Value -1 "LastColumn"]

Returns a Table

BetaAgeLastColumn
John25NY
Paul35CA

Rename the first column to "FirstColumn" and second to "SecondColumn" using a positional list.

      table = Table.from_rows ["Alpha", "Age", "State"] [["John", 25, "NY"], ["Paul", 35, "CA"]]
output = table.rename_columns ["FirstColumn", "SecondColumn"]

Returns a Table

FirstColumnSecondColumnState
John25NY
Paul35CA

Add a prefix to all column names.

       table = Table.from_rows ["Alpha", "Age", "State"] [["John", 25, "NY"], ["Paul", 35, "CA"]]
output = table.rename_columns [Pair.Value (regex "(.*)"), "prefix:$1"]

Returns a Table

prefix:Alphaprefix:Ageprefix:State
John25NY
Paul35CA

For all columns starting with the prefix name=, replace it with key:.

      table = Table.from_rows ["name=Alpha", "name=Beta", "name=Gamma"] [["John", "25", "abc"]]
output = table.rename_columns [Pair.Value (regex "name=(.*)"), "key:$1"]

Returns a Table

key:Alphakey:Betakey:Gamma
John25abc

Errors

  • If a column in columns is not in the input table, a Missing_Input_Columns is raised as an error, unless error_on_missing_columns is set to False, in which case the problem is reported according to the on_problems setting.
  • If any of the new names are invalid, an Invalid_Column_Names error is raised.
  • Other problems are reported according to the on_problems setting:
    • If a column is matched by two selectors resulting in a different name mapping, a Ambiguous_Column_Rename.
    • If in By_Position mode and more names than columns are provided, a Too_Many_Column_Names_Provided.
    • If any of the new names clash either with existing names or each other, a Duplicate_Output_Column_Names.

Remarks

Pattern based renaming

Regex objects can be used within the column_map to do pattern based renaming. Marked groups can be used in the new name by using $1, $2, etc.