0

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
stark
  • 399
  • 2
  • 13
ragethewolf
  • 160
  • 1
  • 10

2 Answers2

2

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)
Mohit Sharma
  • 590
  • 3
  • 10
  • 1 question can u please explain new_col = {c:c+"_"+name for c in df.columns[1:]} kind of a newbie here – ragethewolf Feb 18 '20 at 09:23
  • 1
    @ragethewolf That's called a "dictionary comprehension". Read up on "list comprehensions" if you're not familiar. – Mateen Ulhaq Feb 18 '20 at 09:26
  • 1
    its a dict comprehension, in simple terms: `new_col = {} for c in df.columns[1:]: # it will iterate through columns excluding 1st new_col[c] = c+"_"+name` dict will be created with key (old names) and value (new names) – Mohit Sharma Feb 18 '20 at 09:26
2

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()
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252