How to rename column header of a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to rename column header of a Pandas DataFrame?

How to rename column header of a Pandas DataFrame?

This recipe helps you rename column header of a Pandas DataFrame

0
This python source code does the following: 1. Creates data dictionary and converts it into dataframe 2. Renames the column by using "iloc" selection function
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

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.

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

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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.

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.

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.

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.

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.

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.

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.