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

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.