+2 votes
in Programming Languages by (73.8k points)

Using df.columns =[columns], I can change the name of all columns of a DataFrame, but not some specific columns. I want to rename a couple of columns. How can I do that?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

You can use the rename() function of Pandas. You need to pass a dictionary having the old name as the key and the new name as the value to this function. Here is an example to use this function in different ways:

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({'id1':[1,2,3,4,5,6,7,8,9,10],'class':['pos','pos','neg','pos','neg','neg','pos','neg','neg','pos']})
>>> df
   id1 class
0    1   pos
1    2   pos
2    3   neg
3    4   pos
4    5   neg
5    6   neg
6    7   pos
7    8   neg
8    9   neg
9   10   pos

>>> df1=df.rename(columns={"id1":"protein_id"})
>>> df1
   protein_id class
0           1   pos
1           2   pos
2           3   neg
3           4   pos
4           5   neg
5           6   neg
6           7   pos
7           8   neg
8           9   neg
9          10   pos
>>> df2=df.rename({"id1":"protein_id"}, axis=1)
>>> df2
   protein_id class
0           1   pos
1           2   pos
2           3   neg
3           4   pos
4           5   neg
5           6   neg
6           7   pos
7           8   neg
8           9   neg
9          10   pos
>>> df3=df.rename({"id1":"protein_id"}, axis='columns')
>>> df3
   protein_id class
0           1   pos
1           2   pos
2           3   neg
3           4   pos
4           5   neg
5           6   neg
6           7   pos
7           8   neg
8           9   neg
9          10   pos


...