The ask is simple, for renaming multiple dataframe's with a single code I have written the below but when I am using the code the column which contains the primary key is also getting renamed what I want my code to do is to skip the first column and rename the rest.
Codes have been provided below for better understanding of what I am trying to achieve:
import pandas as pd
USA = pd.read_excel(r"C:\Users\Rage\Desktop\usa.xlsx")
BRA = pd.read_excel(r"C:\Users\Rage\Desktop\usa.xlsx")
CAN = pd.read_excel(r"C:\Users\Rage\Desktop\usa.xlsx")
df_dict = {"USA":USA, "BRA":BRA, "CAN":CAN}
for name, df in df_dict.items():
df.columns = df.columns + "_" + name
Which gives me the below output:
Primary Key_CAN local stat_CAN valid stat_CAN valid date_CAN
123 Approved completed 2018-02-02
554 Restrict pending 2020-06-05
789 Declined pending 2016-08-07
Which is perfect but with one problem I want the loop to skip the first column that is "Primary key" Making my expected output to be below:
Primary Key local stat_CAN valid stat_CAN valid date_CAN
123 Approved completed 2018-02-02
554 Restrict pending 2020-06-05
789 Declined pending 2016-08-07