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

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.

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)

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

Relevant Projects

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

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.

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.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

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.