How to rename multiple column headers in a Pandas DataFrame?

This recipe helps you rename multiple column headers in a Pandas DataFrame

Recipe Objective

If we want to rename some of all the columns then creating a new dataset may not be possible. We can do this by simply few lines of codes.

So this is the recipe on How we can rename multiple column headers in a Pandas DataFrame.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

Step 1 - Import the library

import pandas as pd

We have only imported pandas which is required for this.

Step 2 - Setting up the Data

We have created a dictionary with columns 'Name', 'Comic', 'Episodes' and passed this in pd.DataFrame to create a DataFrame with index. data = {'Name': ['Amy', 'penny', 'Sheldon', 'Raj', 'Leonard'], 'Comic': [8, 7, 10, 2, 8], 'Episodes': [32, 66, 70, 62, 69]} df = pd.DataFrame(data, index = ['a', 'b', 'c', 'd', 'e']) print(df)

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

Step 3 - Renaming the columns and Printing the Dataset

We can change the columns by renaming all the columns by df.columns = ['Character', 'Funny', 'Episodes'] print(df) Or we can rename especific column by creating a dictionary and passing through df.rename with a additional parameter inplace which is bool by default it is False. df.rename(columns={'Character':'Name'}, inplace=True) print(df) Output of the dataset is

      Name  Comic  Episodes
a      Amy      8        32
b    penny      7        66
c  Sheldon     10        70
d      Raj      2        62
e  Leonard      8        69

  Character  Funny  Episodes
a       Amy      8        32
b     penny      7        66
c   Sheldon     10        70
d       Raj      2        62
e   Leonard      8        69

      Name  Funny  Episodes
a      Amy      8        32
b    penny      7        66
c  Sheldon     10        70
d      Raj      2        62
e  Leonard      8        69

Download Materials

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN