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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

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.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.