How to rename multiple column headers in a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to rename multiple column headers in a Pandas DataFrame?

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.

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)

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

Relevant Projects

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

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.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.