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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS