How to rename multiple column headers in a Pandas DataFrame?
0

How to rename multiple column headers in a Pandas DataFrame?

This recipe helps you rename multiple column headers in a Pandas DataFrame

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

Big Data Project Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.
Big Data Project 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.
Big Data Project Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.
Big Data Project Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.
Big Data Project Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.
Big Data Project 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.
Big Data Project 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.
Big Data Project 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.
Big Data Project 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.
Big Data Project Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.