ragethewolf
ragethewolf

Reputation: 158

Skipping the first column while renaming column using for loop in python

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

Upvotes: 0

Views: 1244

Answers (2)

Mohit Sharma
Mohit Sharma

Reputation: 590

This will work for you

for name, df in df_dict.items():
        new_col = {c:c+"_"+name for c in df.columns[1:]}
        df.rename(columns=new_col, inplace = True)

Upvotes: 2

Serge Ballesta
Serge Ballesta

Reputation: 149125

You should split the columns index in 2 sublists and only change the second one:

df.columns = [df.columns[0]] + (df.columns[1:] + '_' + 'CAN').tolist()

Upvotes: 2

Related Questions