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

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.

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.

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.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

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.

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.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

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.

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.