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
In [1]:
## How to rename multiple column headers in a Pandas DataFrame
def Snippet_103():
    print()
    print(format('How to rename multiple column headers in a Pandas DataFrame','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Set ipython's max row display
    pd.set_option('display.max_row', 1000)

    # Set iPython's max column width to 50
    pd.set_option('display.max_columns', 50)

    # Create an example dataframe
    data = {'Commander': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
            'Date': ['2012, 02, 08', '2012, 02, 08', '2012, 02, 08',
                     '2012, 02, 08', '2012, 02, 08'],
            'Score': [4, 24, 31, 2, 3]}
    df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz',
                                     'Maricopa', 'Yuma'])
    print(); print(df)

    # Rename Column Names
    df.columns = ['Leader', 'Time', 'Score']
    print(); print(df)

    # Rename Column Name Leader to Commander
    df.rename(columns={'Leader':'Commander'}, inplace=True)
    print(); print(df)

Snippet_103()
***********How to rename multiple column headers in a Pandas DataFrame************

           Commander          Date  Score
Cochice        Jason  2012, 02, 08      4
Pima           Molly  2012, 02, 08     24
Santa Cruz      Tina  2012, 02, 08     31
Maricopa        Jake  2012, 02, 08      2
Yuma             Amy  2012, 02, 08      3

           Leader          Time  Score
Cochice     Jason  2012, 02, 08      4
Pima        Molly  2012, 02, 08     24
Santa Cruz   Tina  2012, 02, 08     31
Maricopa     Jake  2012, 02, 08      2
Yuma          Amy  2012, 02, 08      3

           Commander          Time  Score
Cochice        Jason  2012, 02, 08      4
Pima           Molly  2012, 02, 08     24
Santa Cruz      Tina  2012, 02, 08     31
Maricopa        Jake  2012, 02, 08      2
Yuma             Amy  2012, 02, 08      3

Relevant Projects

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

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.

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.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

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.

Data Science Project-All State Insurance Claims Severity Prediction
Data science project in R to develop automated methods for predicting the cost and severity of insurance claims.

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

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.