Skip to content
Hassan Syyid edited this page Feb 11, 2021 · 1 revision

rename

Renames columns in DataFrame using a json format

Definition

rename(df, target_columns):
"""
       :param df : dataframe
       :param target_columns: the columns to rename as a dictionary
       :returns df: a modified data frame
"""

Example

Input

input

Objective

Let's clean up the data by renaming the columns to more readable names.

CustomerRef__value -> CustomerId
CustomerRef__name -> Customer
MetaData_LastUpdatedTime -> LastUpdated
MetaData_CreateTime -> CreatedOn
CurrencyRef__name -> Currency
CurrencyRef__value -> CurrencyCode
# Let's clean up the names of these columns
invoices = input_df.pipe(lambda x: x.rename(columns={'CustomerRef__value': 'CustomerId', 'CustomerRef__name': 'Customer',
                                              'MetaData__LastUpdatedTime': 'LastUpdated',
                                              'MetaData__CreateTime': 'CreatedOn', 'CurrencyRef__name': 'Currency',
                                              'CurrencyRef__value': 'CurrencyCode'}))
invoices.head()

Output

output

Clone this wiki locally