30

I am having below pandas dataframe df. I am trying to rename the column names but it not working as expected.

Code:

 mapping = {df.columns[0]:'Date', df.columns[1]: 'A', df.columns[2]:'B', df.columns[3]: 'C',df.columns[4]:'D', df.columns[5]: 'E',df.columns[6]:'F', df.columns[7]: 'G',df.columns[8]:'H', df.columns[9]: 'J'} df.rename(columns=mapping) 

Output of df.columns:

MultiIndex(levels=[['A Index', 'B Index', 'C Index', 'D Index', 'E Index', 'F Index', 'G Index', 'H Index', 'I Index', 'J Index', 'K Index', 'L Index', 'M Index', 'N Index', 'O Index', 'date', 'index'], ['PX_LAST', '']], labels=[[16, 15, 11, 9, 10, 6, 3, 4, 2, 5, 14, 1, 13, 12, 7, 0, 8], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], names=['ticker', 'field']) 

Even after running the code, column name stays the same. Can anyone help rename column names of this dataframe.

3 Answers 3

38

Because you know the order of the columns already why not just use:

df.columns = ['Date', 'a', 'b', 'c', 'd', 'e', 'f' 'g', 'h', 'i', 'j'] 

Otherwise if you want to use rename you will need to assign it to a variable:

 mapping = {df.columns[0]:'Date', df.columns[1]: 'A', df.columns[2]:'B', df.columns[3]: 'C',df.columns[4]:'D', df.columns[5]: 'E',df.columns[6]:'F', df.columns[7]: 'G',df.columns[8]:'H', df.columns[9]: 'J'} df = df.rename(columns=mapping) 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot!. df.columns = ['Date', 'a', 'b', 'c', 'd', 'e', 'f' 'g', 'h', 'i', 'j'] works fine
df.rename(columns=mapping, inplace=True) would also work.
If I use df = df.rename(columns=mapping), I get the error: TypeError: rename() got an unexpected keyword argument 'columns' Do you maybe have a solution for that? thank you
10

Let's assume that you have a mapping such as:

mapping = {"old_name_1" : "new_name_1", "old_name_2" : "new_name_2"} 

Pandas version < 1.4.3

Given the previous documentation from pandas, the default value on axis parameter is 0 (meaning index).

Thus, changing your command to:

df = df.rename(columns = mapping, axis = 1) 

, where axis equal to 1 means columns, will work as expected.

Also, you can use the inplace parameter so you won't have to re-set your DataFrame.

df.rename(columns = mapping, axis = 1, inplace = True) 

Pandas version >= 1.4.3

(thanks @Gordon for the heads-up)

You can just use:

df = df.rename(columns = mapping) 

or

df.rename(columns = mapping, inplace = True) 

With the new update (see documentation), pandas understand that when you set a mapping to columns parameter, you mean to change the value of columns (as it is logical to happen); thus, the axis parameter is unnecessary.

1 Comment

revision of 1.4.3 of pandas restricts this. TypeError: Cannot specify both 'axis' and any of 'index' or 'columns'
5

Adding inplace=True to the rename call will work.

Though, I'm not sure what the result is if I don't specify inplace=True, since I don't see any change.

2 Comments

Please update your answer accordingly to include the explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.