Change multiple column names in pandas dataframe (not all colmn names) at the same time using index numbers

I have successfully changed a single column name in the dataframe using this:

df.columns=['new_name' if x=='old_name' else x for x in df.columns]

However i have lots of columns to update (but not all 240 of them) and I don’t want to have to write it out for each single change if i can help it.

I have tried to follow the advice from @StefanK in this thread:

Changing multiple column names but not all of them – Pandas Python

my code:


but i am getting an error message:

File "<ipython-input-17-2808488b712d>", line 3
SyntaxError: can't assign to literal

So having googled the error and read many more S.O. questions here it looks to me like it is trying to read the numbers as integers instead of an index? I’m not certain here though.

So how do i fix it so it looks at the numbers as the index?! The column names I am replacing are at least 10 words long each so I’m keen not to have to type them all out! my only ideas are to use iloc somehow but i’m going into new territory here!

really appreciate some help please

Stack Overflow Asked by Mizz H on December 27, 2020

2 Answers

2 Answers

Remove the '=' after df.columns in your code and use this instead:


Correct answer by Arvind Kumar on December 27, 2020

Because index does not support mutable operations convert it to numpy array, reassign and set back:

df = pd.DataFrame({

arr = df.columns.to_numpy()
arr[[0,2,3]] = list('RTG')
df.columns = arr
print (df)
   R  B  T  G  E  F
0  a  4  7  1  5  a
1  b  5  8  3  3  a
2  c  4  9  5  6  a
3  d  5  4  7  9  b
4  e  5  2  1  2  b
5  f  4  3  0  4  b

So with your data use:

idx = [4,18,181,182,187,188,189,190,203,204]
names = ['Brand','Reason','Chat_helpful','Chat_expertise','Answered_questions','Recommend_chat','Alternate_help','Customer_comments','Agent_category','Agent_outcome']

arr = df.columns.to_numpy()
arr[idx] = names
df.columns = arr

Answered by jezrael on December 27, 2020

Add your own answers!

Related Questions

Remove web-inspector-hide-shortcut

0  Asked on January 9, 2021 by dumi-padureanu


Pandas – Lambda inside apply to return a row

1  Asked on January 9, 2021 by marcio


Unclear webhook eventNotification documentation

1  Asked on January 9, 2021 by nolan-aguirre


c – Access not within mapped region error

0  Asked on January 9, 2021 by rhcp_lean


How to reduce committed memory usage in Java

1  Asked on January 9, 2021 by simon-hu


mutool / mulib edit text (with a c programm)

1  Asked on January 9, 2021 by atticus


Running browsermob with selenium grid on docker

2  Asked on January 8, 2021 by eduardoreynoso


How do I exclude a repeated click event?

2  Asked on January 8, 2021 by jessy


handling promises using await and async

2  Asked on January 8, 2021 by jass-preet


How to compare data from the same column in a dataframe (Pandas)

4  Asked on January 8, 2021 by abraham-arreola


Ask a Question

Get help from others!

© 2022 All rights reserved.