How to map values in a Pandas DataFrame?

This recipe helps you map values in a Pandas DataFrame

Recipe Objective - How to map values in a Pandas DataFrame?

We sometimes use Python Pandas to map values to other values in Python, i.e., values of a feature with values of another feature.

This recipe will show you how to perform Pandas Dataframe map column values.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

Steps For Python Pandas Map Column Values

The following steps will help you understand how to map Pandas dataframe, i.e., map column values in Pandas Dataframe.

Step 1 - Import the library

import pandas as pd

We have imported the Pandas library, which is needed to perform Pandas Dataframe map values.

Step 2 - Setting up the Data

We have created a dataset by making a dictionary with features and passing it through the dataframe function. 

raw_data = {"first_name": ["Sheldon", "Raj", "Leonard", "Howard", "Amy"], "last_name": ["Copper", "Koothrappali", "Hofstadter", "Wolowitz", "Fowler"], "age": [42, 38, 36, 41, 35], "Comedy_Score": [9, 7, 8, 8, 5], "Rating_Score": [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ["first_name", "last_name", "age", "Comedy_Score", "Rating_Score"]) print(df)

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

Step 3 - Pandas Dataframe: Map Values

First, we have made a dictionary with the values mapped with other values, such that the first value is of feature first_name and the next is of new feature subjects. 

Subjects =
{"Sheldon" : "Science", "Raj" : "Chemistry", "Leonard" : "Maths", "Howard" : "Astronaut", "Amy" : "Science"} print(Subjects) 

Now, we have created a function to map the values of different columns. 

df["Subjects"] = df["first_name"].map(Subjects) print(df) 

So the output comes as-

 first_name     last_name  age  Comedy_Score  Rating_Score

0    Sheldon        Copper   42             9            25

1        Raj  Koothrappali   38             7            25

2    Leonard    Hofstadter   36             8            49

3     Howard      Wolowitz   41             8            62

4        Amy        Fowler   35             5            70

 

{"Sheldon": "Science", "Raj": "Chemistry", "Leonard": "Maths", "Howard": "Astronaut", "Amy": "Science"}

 

  first_name     last_name  age  Comedy_Score  Rating_Score   Subjects

0    Sheldon        Copper   42             9            25    Science

1        Raj  Koothrappali   38             7            25  Chemistry

2    Leonard    Hofstadter   36             8            49      Maths

3     Howard      Wolowitz   41             8            62  Astronaut

4        Amy        Fowler   35             5            70    Science

Pandas DataFrame- Map Column Values to Lowercase

To map values in a Pandas DataFrame to lowercase, you can use the str.lower() method. The str.lower() method converts a string to lowercase.

The following code shows how to map values in a Pandas DataFrame to lowercase-

import pandas as pd

df = pd.DataFrame({'A': ['Hello', 'World', 'PYTHON']})

# Map the values in column A to lowercase

df['A'] = df['A'].str.lower()

print(df)

The output of the code is shown below:

       A

0   hello

1   world

2   python




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

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

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.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.