News

Python DataFrame – Assign New Labels to Columns

In this post, you will get a code sample related to how to assign new labels to columns in python programming while training machine learning models. 

This is going to be very helpful when working with classification machine learning problem. Many a time the labels for response or dependent variable are in text format and all one wants is to assign a number such as 0, 1, 2 etc instead of text labels. Beginner-level data scientists will find this code very handy.

We will look at the code for the dataset as represented in the diagram below:

Fig 1. Python – Read CSV File

In the above code, you will see that class labels are named as very_low, Low, High, Middle etc. The objective is to provide label as 1, 2, 3, 4 etc.

Code for Assigning New Class Labels

Here are the steps:

Define a function which returns different numeric value based on the label

def assignNewLabels(label):
    if label == 'very_low' or label == 'Very Low':
        return 1
    elif label == 'Low':
        return 2
    elif label == 'Middle':
        return 3
    elif label == 'High':
        return 4
    else:
        return -1

Next step is to use apply method

df1['UNS_NEW'] = df1['UNS'].apply(assignNewLabels)

The above code creates a new column namely UNS_NEW. The data frame looks like the following:

Fig 2. Python Data frame columns – Labels assigned new value

Note that there is a new column with new labels (1, 2, 3, 4) assigned based on the logic within assignNewLabels function.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

1 week ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

2 weeks ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

2 weeks ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

3 weeks ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

3 weeks ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

3 weeks ago