6 different ways to rename column names in pandas DataFrame
In this article, I will discuss the different ways to rename the column names in pandas DataFrame
First, create a pandas DataFrame
import pandas as pd
df = pd.DataFrame({'col1':['A', 'B', 'C', 'D', 'E'], 'col2':[1, 2, 3, 4, 5], 'col3':[0.1, 0.2, 0.3, 0.4, 0.5]})
df.head(2)
# output
col1 col2 col3
0 A 1 0.1
1 B 2 0.2
Now, let’s check different methods to rename the column names in pandas DataFrame
1. Rename column names using DataFrame columns
attribute
In this method, you can rename all columns of pandas DataFrame
df.columns = ['A', 'B', 'C']
df.head(2)
# output
A B C
0 A 1 0.1
1 B 2 0.2
2. Rename column names using DataFrame rename()
function
In this method, you can rename all or specific columns of pandas DataFrame
Change column names for all columns,
df.rename(columns={"col1": "A", "col2": "B", "col3": "C"}, inplace=True) # use inplace=True to modify and update the current DataFrame
df.head(2)
# output
A B C
0 A 1 0.1
1 B 2 0.2
Change column names for specific columns
df.rename(columns={"col1": "A", "col3": "C"}, inplace=True) # use inplace=True to modify and update the current DataFrame
df.head(2)
# output
A col2 C
0 A 1 0.1
1 B 2 0.2
Change column names to uppercase or lowercase letters
df.rename(columns=str.upper, inplace=True) # use columns=str.lower for lowercase letters
df.head(2)
# output
COL1 COL2 COL3
0 A 1 0.1
1 B 2 0.2
3. Rename column names using DataFrame dfply
package
dfply
Python package is similar to R’s dplyr
and supports the data manipulation with pipes on
pandas DataFrame
rename()
function will rename columns (as provided by key) corresponding to old columns (provided by values)
from dfply import *
# rename columns
df >> rename(A='col1', B='col2', C='col3') >> head(2)
A B C
0 A 1 0.1
1 B 2 0.2
4. Rename column names using DataFrame set_axis()
function
df.set_axis(['A', 'B', 'C'], axis='columns', inplace=True)
df.head(2)
# output
A B C
0 A 1 0.1
1 B 2 0.2
5. Rename column names using DataFrame add_prefix()
and add_suffix()
functions
Change column names by adding prefix or suffix to each column name
# add prefix
df = df.add_prefix('P_')
df.head(2)
# output
P_col1 P_col2 P_col3
0 A 1 0.1
1 B 2 0.2
# add suffix
df = df.add_suffix('_S')
df.head(2)
# output
col1_S col2_S col3_S
0 A 1 0.1
1 B 2 0.2
You can also use lambda
function to add suffix or prefix to column names,
# add prefix
df.rename(columns=lambda c: 'P_'+c, inplace=True)
df.head(2)
# output
P_col1 P_col2 P_col3
0 A 1 0.1
1 B 2 0.2
# add suffix
df.rename(columns=lambda c: c+'_S', inplace=True)
df.head(2)
# output
col1_S col2_S col3_S
0 A 1 0.1
1 B 2 0.2
6. Replace specific texts of column names
In this method, you can replace specific strings in a column names
# replace col to COL
df.columns = df.columns.str.replace('col', 'COL')
df.head(2)
# output
COL1 COL2 COL3
0 A 1 0.1
1 B 2 0.2
Enhance your skills with courses on Python and pandas
- Python for Data Analysis: Pandas & NumPy
- Mastering Data Analysis with Pandas: Learning Path Part 1
- Data Analysis Using Python
Learn more about Python
If you have any questions, comments or recommendations, please email me at reneshbe@gmail.com
This work is licensed under a Creative Commons Attribution 4.0 International License