How to rename column header of a Pandas DataFrame?

This recipe helps you rename column header of a Pandas DataFrame

Recipe Objective

Have you ever tried to rename a column in pandas dataframe by header. That is setting first element of column as the name of the column.

So this is the recipe on how we can rename column header of a Pandas DataFrame.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

Step 1 - Import the library

import pandas as pd

We have imported pandas which will be needed for the dataset.

Step 2 - Setting up the Data

We have created a dataframe with features as "0", "1", "2" and "3". raw_data = {"0": ["first_name", "Penny", "Tina", "Jake", "Amy"], "1": ["last_name", "Jacobson", "Raj", "Milner", "Cooze"], "2": ["age", 52, 38, 27, 12], "3": ["Rating_Score", 29, 51, 25, 13]} df = pd.DataFrame(raw_data) print(df)

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

Step 3 - Renaming column with header

So Here first we have selected the first element of the columns and stored it in header and then we have selected the rest of the elements of the dataframe. Now we are renaming the columns with the values we have stored in the header. header = df.iloc[0] print(header) df = df[1:] print(df) df = df.rename(columns = header) print(df) So the output comes as:

            0          1    2             3
0  first_name  last_name  age  Rating_Score
1       Penny   Jacobson   52            29
2        Tina        Raj   38            51
3        Jake     Milner   27            25
4         Amy      Cooze   12            13

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

       0         1   2   3
1  Penny  Jacobson  52  29
2   Tina       Raj  38  51
3   Jake    Milner  27  25
4    Amy     Cooze  12  13

  first_name last_name age Rating_Score
1      Penny  Jacobson  52           29
2       Tina       Raj  38           51
3       Jake    Milner  27           25
4        Amy     Cooze  12           13

Download Materials

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.