rename() method
Renames one or more columns in a dataframe.
Syntax
dataframe_name.rename({"old_column_name":"new_column_name", "...n":"...n"}|dictionary_object)
Parameters
Name | Description |
---|---|
{"old_column_name" : "new_column_name", "...n" : "...n"} |
A mapping between an existing column name and a new name that you want to apply to the column. Construct the mapping for one or more columns using a standard Python dictionary. |
dictionary_object |
A pre-existing dictionary object that maps one or more existing column names to new names. As an alternative to creating the column mapping inside rename(), you can create the mapping in a separate dictionary and specify only the dictionary name inside rename(). If you need to rename the same columns in multiple dataframes, this approach is more efficient. |
Returns
HCL dataframe.
Examples
Rename columns in a dataframe
You rename three columns in the accounts_receivable dataframe:
- No becomes Customer Number
- Date becomes Invoice Date
- Due becomes Due Date
accounts_receivable_2 = accounts_receivable.rename({"No":"Customer Number", "Date":"Invoice Date", "Due":"Due Date"})
Rename columns in two dataframes using a pre-existing dictionary
You rename columns in two dataframes using the same dictionary object ( updated_col_names ) for both rename operations.
updated_col_names = {"No":"Customer Number", "Date":"Invoice Date", "Due":"Due Date"}
accounts_Jan_renamed = accounts_Jan.rename(updated_col_names)
accounts_Feb_renamed = accounts_Feb.rename(updated_col_names)