TransWikia.com

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

Stack Overflow Asked by Mizz H on December 27, 2020

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:

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

but i am getting an error message:

File "<ipython-input-17-2808488b712d>", line 3
    df.columns=[[4,18,181,182,187,188,189,190,203,204]]=['Brand','Reason','Chat_helpful','Chat_expertise','Answered_questions','Recommend_chat','Alternate_help','Customer_comments','Agent_category','Agent_outcome']
                   ^
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

2 Answers

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

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

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({
        'A':list('abcdef'),
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],
         'E':[5,3,6,9,2,4],
         'F':list('aaabbb')
})

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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP