ColumnRename
Renames one or more columns in a dataframe through one of the following methods:
- Entirely change the name of a column
- Add a suffix to one or more columns
- Add a prefix to one or more columns
- Change matching portions of one or more column names to a new value
- Entirely change the name of a column
- Add a suffix to one or more columns
- Add a prefix to one or more columns
- Change matching portions of one or more column names to a new value
Options
columns: Specifies columns to rename
to: Specifies the new column names
suffix: Specifies a suffix to add to column names
prefix: Specifies a prefix to add to column names
replace: Use with --with, specifies the portion of column names to replace
with: Use with --replace, specifies the replacement value for column names
Examples
Example 1 - Rename a Single Column
Rename a specific column to a new name by directly specifying its current column name. In this example, we change the column CustomerAge to Age. This can be helpful for making column names shorter or more intuitive to work with.
#> ColumnRename CustomerAge --to Age
AFLEFT
bankTransactionsDf = bankTransactionsDf.rename(columns={'CustomerAge': 'Age'}) AFRIGHT
Example 2 - Rename Multiple Columns by Name
Rename multiple columns by providing their original names followed by the new names. This is useful when you want to clean up or shorten several column names in one operation. In this case, we rename TransactionID and AccountID to tid and aid.
#> ColumnRename TransactionID AccountID --to tid aid
AFLEFT
bankTransactionsDf = bankTransactionsDf.rename(columns={'TransactionID': 'tid', 'AccountID': 'aid'}) AFRIGHT
Example 3 - Replace Text in All Column Names
Rather than renaming columns one by one, sometimes it is easier to update column names in bulk by replacing part of the name. This example replaces every instance of the substring ID with Identifier across all column names.
#> ColumnRename --replace ID --with Identifier
AFLEFT
bankTransactionsDf.columns = bankTransactionsDf.columns.str.replace('ID', 'Identifier') AFRIGHT
Example 4 - Replace Text in Specific Column Names
This variation of the previous example limits the replacement to only certain columns. In this case, we replace the word Transaction with Payment, but only for the columns TransactionAmount and TransactionType. All other column names are left unchanged.
#> ColumnRename --columns TransactionAmount TransactionType --replace Transaction --with Payment
AFLEFT
bankTransactionsDf = bankTransactionsDf.rename(columns={'TransactionAmount': 'TransactionAmount'.replace('Transaction', 'Payment'), 'TransactionType': 'TransactionType'.replace('Transaction', 'Payment')}) AFRIGHT