Sejal Mohata
Sejal Mohata

Reputation: 23

How do I rename specific columns in one data frame based on another

I have one dataframe as follows:

df1: ACCOUNT_NAME Units GM 1. Sejal 12 12 2. Mohata 10 15

And another dataframe as:

df2: INPUT_FIELD TRANSL 1. ACCOUNT_NAME Account Name 2. GM Gross Margin 3. REVENUE Revenue 4. BU Business Unit

I want to rename the columns of df1 to the TRANSL values in df2, if the values are present in INPUT_FIELD.

I have tried: df1.columns = df1.columns.map(df2.set_index('INPUT_FIELD')['TRANSL'].get)

It renames the columns as: ['Account Name', None, 'Gross Margin']

I want to rename the columns selectively without using equal operator, as this table might grow in the future.

Upvotes: 2

Views: 170

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148880

You can easily build the dict comprehension from df2.values:

df1.rename(columns={i:j for i,j in df2.values})

gives:

    Account Name  Units  Gross Margin
1.0        Sejal     12            12
2.0       Mohata     10            15

Rename also accept a mapper function, so you can use:

def mapper(x):
    trans = df2.loc[df2.INPUT_FIELD == x, 'TRANSL']
    return trans.iat[0] if len(trans)>0 else x
df1.rename(columns = mapper)

In my tests (using timeit) it is longer than the dict way. It is only faster when you have more than 1000 rows in df2 and when the column names are in the first rows, because then it does not scan the whole values array.

Upvotes: 0

Shubham Sharma
Shubham Sharma

Reputation: 71689

You can use Series.to_dict method to convert a series df2["TRANSL"] to a dictionary mappings and then you can use DataFrame.rename method to rename the columns of df1 by passing the mappings dict to columns argument of rename method.

Use:

mappings = df2.set_index("INPUT_FIELD")["TRANSL"].to_dict()
df1.rename(columns=mappings, inplace=True)
print(df1)

This prints:

  Account Name  Units  Gross Margin
0        Sejal     12            12
1       Mohata     10            15

Upvotes: 1

Related Questions