Rename Specific Columns
Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# Or rename the existing DataFrame (rather than creating a copy)
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
Minimal Code Example
df = pd.DataFrame('x', index=range(3), columns=list('abcde'))
df
a b c d e
0 x x x x x
1 x x x x x
2 x x x x x
The following methods all work and produce the same output:
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1)
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')
df2 = df.rename(columns={'a': 'X', 'b': 'Y'})
df2
X Y c d e
0 x x x x x
1 x x x x x
2 x x x x x
Remember to assign the result back, as the modification is not-inplace. Alternatively, specify inplace=True:
df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)
df
X Y c d e
0 x x x x x
1 x x x x x
2 x x x x x
You can specify errors='raise' to raise errors if an invalid column-to-rename is specified.
Reassign Column Headers
Use df.set_axis() with axis=1.
df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1)
df2
V W X Y Z
0 x x x x x
1 x x x x x
2 x x x x x
Headers can be assigned directly:
df.columns = ['V', 'W', 'X', 'Y', 'Z']
df
V W X Y Z
0 x x x x x
1 x x x x x
2 x x x x x
Answer from lexual on Stack OverflowRename Specific Columns
Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# Or rename the existing DataFrame (rather than creating a copy)
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
Minimal Code Example
df = pd.DataFrame('x', index=range(3), columns=list('abcde'))
df
a b c d e
0 x x x x x
1 x x x x x
2 x x x x x
The following methods all work and produce the same output:
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1)
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')
df2 = df.rename(columns={'a': 'X', 'b': 'Y'})
df2
X Y c d e
0 x x x x x
1 x x x x x
2 x x x x x
Remember to assign the result back, as the modification is not-inplace. Alternatively, specify inplace=True:
df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)
df
X Y c d e
0 x x x x x
1 x x x x x
2 x x x x x
You can specify errors='raise' to raise errors if an invalid column-to-rename is specified.
Reassign Column Headers
Use df.set_axis() with axis=1.
df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1)
df2
V W X Y Z
0 x x x x x
1 x x x x x
2 x x x x x
Headers can be assigned directly:
df.columns = ['V', 'W', 'X', 'Y', 'Z']
df
V W X Y Z
0 x x x x x
1 x x x x x
2 x x x x x
Just assign it to the .columns attribute:
>>> df = pd.DataFrame({'
b': [10,20]})
>>> df
b
0 1 10
1 2 20
>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20
How to replace column names with Pandas?
How do I rename columns if they begin with a phrase
Pandas Rename Column does not work
Renaming multiple column headers with nan value [Pandas]
How do I rename a single column in pandas?
How do I rename all columns in a pandas DataFrame?
How do I make all column names lowercase in pandas?
I'm working on a very large dataset (from an Excel document) that has the price information of 415 different products for each month since January 2003. For example, when you open the Excel document the first six months look like below.
2003 2003 2003 2003 2003 2003
January February March April May June
When I used the read_excel() method with the header parameter as follows df.read_excel(header=5), the months in 2004 are read as January.1, February.1 etc. Similarly headers for 2005 look like January.2, February.2 and so on.
When I was using a small portion of the data I just created a dictionary for the old headers as keys and new headers as values and used products.rename(columns=dict_name) but now I want to work on the whole document, which has more than 200 headers but I don't want to rename them individually.
I was wondering if there is an easy way to rename all headers with something like find and replace all that includes ".1" for 2004 for example. so that they reflect their respective years along with the months.
I tried to explain the best I could and hope I could explain what I have in my mind.