Pandas; Method Rename; Columns

Hello :slight_smile:,

I am trying to speed up the process in renaming cols with the rename method from pandas. Like this for example:

for n in np.arange(16,29):
   col = 'Unnamed: {}'.format(n)
   

star_wars = star_wars.rename(columns={
'Please state whether you view the following characters favorably, unfavorably, or are unfamiliar with him/her.': 'Han Solo',
col: [
   'Luke Skywalker',
   'Princess Leia Organa',
   'Anakin Skywalker',
   'Obi Wan Kenobi',
   'Emperor Palpatine',
   'Darth Vader',
   'Lando Calrissian',
   'Boba Fett',
   'C-3P0',
   'R2 D2',
   'Jar Jar Binks',
   'Padme Amidala',
   'Yoda']}).copy()

Or Like this:

star_wars = star_wars.rename(columns={
    star_wars.columns[15:29]: 
   [ 'Luke Skywalker',
    'Princess Leia Organa',
    'Anakin Skywalker',
    'Obi Wan Kenobi',
    'Emperor Palpatine',
    'Darth Vader',
    'Lando Calrissian',
    'Boba Fett',
    'C-3P0',
    'R2 D2',
    'Jar Jar Binks',
    'Padme Amidala',
    'Yoda']}).copy()

I also tried other solutions, but in every solution I get similar errors:

TypeError: unhashable type: 'Index'
TypeError: unhashable type: 'list'

I tried also also this solution I saw in stack:

But only to found out that after I managed to solve a solution, I could not use this indexes as indexes, if I make myself clear!

So my question is, is there any practical and logical solution to go around this problem, or does one have to type the cols names one by one??

Thanks for your help in advance :slight_smile:

Kind Regards,

Abel

What are you trying to achieve in the 1st two code attempts?
I don’t understand the for n in np.arange(16,29): part.

Maybe this gives a hint: https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary