Shawn
Shawn

Reputation: 603

Using lists in Pandas to replace column names

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

Upvotes: 8

Views: 21704

Answers (3)

mikey
mikey

Reputation: 1210

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.

Upvotes: 5

xmduhan
xmduhan

Reputation: 1025

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')

Upvotes: 2

sparc_spread
sparc_spread

Reputation: 10843

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.

Upvotes: 19

Related Questions