Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lists in Pandas to replace column names

Tags:

python

pandas

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
like image 744
Shawn Avatar asked Jun 01 '17 00:06

Shawn


People also ask

How do I replace a column in pandas?

In order to replace a value in Pandas DataFrame, use the replace() method with the column the from and to values. Below example replace Spark with PySpark value on the Course column. Notice that all the Spark values are replaced with the Pyspark values under the first column.

How do I replace multiple column values in pandas?

To replace multiple values in a DataFrame, you can use DataFrame. replace() method with a dictionary of different replacements passed as argument.


2 Answers

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')
like image 45
xmduhan Avatar answered Sep 22 '22 08:09

xmduhan


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.

like image 194
sparc_spread Avatar answered Sep 22 '22 08:09

sparc_spread