How to map values in Pandas DataFrame in Python

Hits: 925

How to map values in Pandas DataFrame in Python

Mapping values in a Pandas DataFrame is a common data wrangling task. One way to do this is by using the map() function.

The map() function is a built-in function in Python that applies a given function to all items in an input list, tuple or other iterable, and returns a new list containing the results. In the context of a Pandas DataFrame, the map() function can be used to apply a function to one or more columns of the DataFrame.

Here’s an example of how to use the map() function to map values in a Pandas DataFrame:

import pandas as pd 
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
df['A'] = df['A'].map(square)
As you can see in this example, the map() function is used to apply the square() function to the values in column ‘A’ of the DataFrame. The result is a new DataFrame with the values in column ‘A’ squared.

It’s also possible to use map with a dictionary. In this case it will map the keys of the dictionary to the corresponding values.

data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
mapping = {1: 'One', 2: 'Two', 3: 'Three'}

df["A"] = df["A"].map(mapping)


In this way, you can map the values of any column in a DataFrame using the map() function. It can be a powerful tool when working with large datasets.

Additionally, Pandas also provides replace() function that can be use to replace values in the DataFrame. This function can take a dictionary to replace multiple values at once or a single value or even a list of values. It can also replace the values based on a condition by passing inplace=True or inplace=False and is more efficient when you have large DataFrame.

In this Learn through Codes example, you will learn: How to map values in Pandas DataFrame in Python.



Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners