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 aDictionaryor aVectorof pairs of old and new names. ATablecan 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 theon_problemssettings. Defaults toTrue.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
| Delta | Beta |
|---|---|
| John | 25 |
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
| Alpha | LastColumn |
|---|---|
| John | 25 |
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
| Beta | Age | LastColumn |
|---|---|---|
| John | 25 | NY |
| Paul | 35 | CA |
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
| FirstColumn | SecondColumn | State |
|---|---|---|
| John | 25 | NY |
| Paul | 35 | CA |
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:Alpha | prefix:Age | prefix:State |
|---|---|---|
| John | 25 | NY |
| Paul | 35 | CA |
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:Alpha | key:Beta | key:Gamma |
|---|---|---|
| John | 25 | abc |
Errors
- If a column in
columnsis not in the input table, aMissing_Input_Columnsis raised as an error, unlesserror_on_missing_columnsis set toFalse, in which case the problem is reported according to theon_problemssetting. - If any of the new names are invalid, an
Invalid_Column_Nameserror is raised. - Other problems are reported according to the
on_problemssetting:- If a column is matched by two selectors resulting in a different
name mapping, a
Ambiguous_Column_Rename. - If in
By_Positionmode and more names than columns are provided, aToo_Many_Column_Names_Provided. - If any of the new names clash either with existing names or each
other, a
Duplicate_Output_Column_Names.
- If a column is matched by two selectors resulting in a different
name mapping, a
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.