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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Microsoft Fabric end-to-end Data Engineering project Overview
In this Microsoft Fabric project, you'll build a financial reporting agent that simplifies data management, automates analysis, and delivers real-time dashboards for wealth advisors and their clients.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

AI Video Summarization Project using Mixtral, Whisper, and AWS
In this AI Video Summarization Project, you will build a quiz generation tool by extracting key concepts from educational videos and generating concise summaries.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.