malligator
malligator

Reputation: 129

Pandas Dataframe - rename columns based on the dataframe name

I want to write a function that updates the column names of a df based on the name of the df.

I have a number of dfs with identical columns. I need eventually to merge these dfs into one df . To identify where the data has originally come from once merged I want to update the column names by appending an identifier to the column name in each separate df first

I have tried using a dictionary (dict) within the function to update the columns but have been unable to get this to work

I have attempted the following function:

def update_col(input):

    dict = {'df1': 'A'
            ,'df2': 'B'
            }
    input.rename(columns= {'Col1':'Col1-' + dict[input]
                            ,'Col2':'Col2-' + dict[input]
                            },inplace= True)

My test df are

df1:

Col1  Col2
foo   bah
foo   bah

df2:

Col1  Col2
foo   bah
foo   bah

Running the function as follows I wish to get:

update_col(df1)

df1:

Col1-A  Col2-A
foo     bah
foo     bah

Upvotes: 1

Views: 97

Answers (1)

anky
anky

Reputation: 75080

I think better way would be:

mydict = {'df1': 'A'
        ,'df2': 'B'
        }

d={'df'+str(e+1):i for e,i in enumerate([df1,df2])} #create a dict of dfs

final_d={k:v.add_suffix('-'+v1) for k,v in d.items() for k1,v1 in mydict.items() if k==k1}
print(final_d)

{'df1':   Col1-A Col2-A
0    foo    bah
1    foo    bah, 'df2':   Col1-B Col2-B
0    foo    bah
1    foo    bah}

you can then access the dfs as final_d['df1'] etc.

Note: Please dont use dict as a dictionary name as it is a builtin python function

Upvotes: 2

Related Questions