How to rename column header of a Pandas DataFrame?
0

How to rename column header of a Pandas DataFrame?

This recipe helps you rename column header of a Pandas DataFrame

In [1]:
## How to rename column header of a Pandas DataFrame
def Snippet_102():
    print()
    print(format('How to rename column header of a Pandas DataFrame','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Create a values as dictionary of lists
    raw_data = {'0': ['first_name', 'Molly', 'Tina', 'Jake', 'Amy'],
                '1': ['last_name', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                '2': ['age', 52, 36, 24, 73],
                '3': ['preTestScore', 24, 31, 2, 3]}

    # Create a dataframe
    df = pd.DataFrame(raw_data)

    # View a dataframe
    print(); print(df)

    # Create a new variable called 'header' from the first row of the dataset
    header = df.iloc[0]
    print(); print(header)

    # Replace the dataframe with a new one which does not contain the first row
    df = df[1:]
    print(); print(df)

    # Rename the dataframe's column values with the header variable
    df = df.rename(columns = header)
    print(); print(df)

Snippet_102()
****************How to rename column header of a Pandas DataFrame*****************

            0          1    2             3
0  first_name  last_name  age  preTestScore
1       Molly   Jacobson   52            24
2        Tina        Ali   36            31
3        Jake     Milner   24             2
4         Amy      Cooze   73             3

0      first_name
1       last_name
2             age
3    preTestScore
Name: 0, dtype: object

       0         1   2   3
1  Molly  Jacobson  52  24
2   Tina       Ali  36  31
3   Jake    Milner  24   2
4    Amy     Cooze  73   3

  first_name last_name age preTestScore
1      Molly  Jacobson  52           24
2       Tina       Ali  36           31
3       Jake    Milner  24            2
4        Amy     Cooze  73            3

Relevant Projects

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 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 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.
Big Data Project Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.
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 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.
Big Data Project 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.
Big Data Project 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.
Big Data Project 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.
Big Data Project Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.