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

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

Download Materials

Relevant Projects

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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.

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.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

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.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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

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.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.