9

I'm trying to understand my mistake when using df.rename in pandas. Specifically, using the rename function with a tuple executes without error, but no changes are made to the column names.

f_GreaterArea = pd.DataFrame(np.random.randn(5, 3), 
                index=['a', 'c', 'e', 'f', 'h'],
                columns=['one', 'two', 'three'])

print(f_GreaterArea)

    one       two     three
a  0.278969 -0.676388 -2.464444
c -0.992077 -0.435534  2.267315
e  2.094669 -1.401885  1.243658
f  0.886835  0.195726 -0.132382
h -0.920486 -0.298380  2.227378

old_colnames = ('one', 'two', 'three')
new_colnames = ('pig', 'cups', 'seven')


f_GreaterArea.rename(columns={old_colnames:new_colnames}, inplace=True)

print(f_GreaterArea)

    one       two     three
a  0.278969 -0.676388 -2.464444
c -0.992077 -0.435534  2.267315
e  2.094669 -1.401885  1.243658
f  0.886835  0.195726 -0.132382
h -0.920486 -0.298380  2.227378
2
  • 1
    Is there a reason you need to use a tuple? Commented Jun 1, 2017 at 0:35
  • 1
    Actually, what I wanted to use were lists (and sparc_spread's answer allows this). The reason is that the lists are important later (e.g., specifying dropna columns, etc..) Commented Jun 1, 2017 at 0:52

3 Answers 3

19

You are correct in wanting to pass in a dict with three entries, one for each column you are renaming, but the dict you are passing is not. It's a dict of one entry, with one tuple as a key and one as a value.

Use a dict comprehension to turn the tuples into a dict, like this:

{i:j for i,j in zip(old_colnames,new_colnames)}

So in the context of your code, that's:

col_rename_dict = {i:j for i,j in zip(old_colnames,new_colnames)}
f_GreaterArea.rename(columns=col_rename_dict, inplace=True)

Or just:

f_GreaterArea.rename(
    columns={i:j for i,j in zip(old_colnames,new_colnames)}, inplace=True
)

Here's a nice little write-up on comprehensions in general, including the dict comprehension. It also includes usage of zip.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help & thanks for accepting! Make sure to check out that link; comprehensions and zip are useful for all sorts of stuff.
6

In python 3.8 and later you can simply assign the column names as a list

f_GreaterArea.columns = ['pig', 'cups', 'seven']

I know the question asks for a tuple, but from the comments it seems that the OP was trying to use a list.

Comments

2

columns parameter should be this way:

{'one': 'pig', 'three': 'seven', 'two': 'cups'}

use this code to get it:

dict(zip(old_colnames, new_colnames))

If you want to change name of columns, use this code will be more easy:

f_GreaterArea.columns = ('pig', 'cups', 'seven')

1 Comment

I see how this technique works as well, and also uses the zip technique, as in sparc_spread's accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.