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

0

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

Relevant Projects

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

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.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

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.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.