• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Uncategorized / Pandas map: Change Multiple Column Values with a Dictionary

Pandas map: Change Multiple Column Values with a Dictionary

April 30, 2021 by cmdlinetips

In this tutorial, we will learn how to use Pandas map() function to replace multiple column values using a dictionary. Earlier, we saw how to use Pandas replace() function to change the values in multiple columns using dictionary. As we all know, there are multiple solutions to a problem.

Pandas map() function works with Pandas Series not Dataframe directly. As Pandas documentation define Pandas map() function is

Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series.

Therefore, here we use Pandas map() with Pandas reshaping functions stack() and unstack() to substitute values from multiple columns with other values using dictionary. In our dictionary, the keys specify column values that we want to replace and values in the dictionary specify what we want in the dataframe.

Pandas Change Multiple Columns Values with map
Pandas Change Multiple Columns Values with map

We will use Pandas’s replace() function to change multiple column’s values at the same time. Let us first load Pandas.

import pandas as pd
# import random 
from random import sample

Let us create some data as before using sample from random module.

# Create two lists in Python
name_list = ["name1", "name2","name3","name4"]

Using the name list, let us create three variables using sample() function and create a dataframe with three columns.

cluster1 = sample(name_list,4)
cluster2 = sample(name_list,4)
cluster3 = sample(name_list,4)
df = pd.DataFrame({"cluster1":cluster1,
              "cluster2":cluster2,
              "cluster3":cluster3,
             })
df

Our dataframe looks like this.

	cluster1	cluster2	cluster3
0	name1	name1	name4
1	name4	name3	name1
2	name3	name4	name3
3	name2	name2	name2

Let us create a dictionary using zip() function to change values in multiple columns in the dictionary.

symbol_list = ["Symbol1", "Symbol2","Symbol3","Symbol4"]
# create a dictionary
n2s = dict(zip(name_list,symbol_list))
n2s

{'name1': 'Symbol1',
 'name2': 'Symbol2',
 'name3': 'Symbol3',
 'name4': 'Symbol4'}

We will use the common idea of reshaping wide dataset into ling form and then use map() function to substitute the values using a dictionary and then reshape back to our original dataframe’s shape.

Let us first see the result of applying stack() function

df.stack()

0  cluster1    name1
   cluster2    name4
   cluster3    name2
1  cluster1    name4
   cluster2    name1
   cluster3    name4
2  cluster1    name2
   cluster2    name3
   cluster3    name3
3  cluster1    name3
   cluster2    name2
   cluster3    name1
dtype: object

Now, let us see the result of using map() to replace column values after using stack().

df.stack().map(n2s)

0  cluster1    Symbol1
   cluster2    Symbol4
   cluster3    Symbol2
1  cluster1    Symbol4
   cluster2    Symbol1
   cluster3    Symbol4
2  cluster1    Symbol2
   cluster2    Symbol3
   cluster3    Symbol3
3  cluster1    Symbol3
   cluster2    Symbol2
   cluster3    Symbol1
dtype: object

Now that we have substituted the values of columns, now we can apply unstack() to reshape back to wide form from tidy form. And we get a dataframe with replaced values.

df.stack().map(n2s).unstack()


cluster1	cluster2	cluster3
0	Symbol1	Symbol4	Symbol2
1	Symbol4	Symbol1	Symbol4
2	Symbol2	Symbol3	Symbol3
3	Symbol3	Symbol2	Symbol1

Want to get better at using Pandas for data science-ing? Check out Byte Sized Pandas 101 tutorials.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Pandas Replace Multiple Column Values with DictionaryHow to Replace Multiple Column Values with Dictionary in Python Default ThumbnailHow To Add a New Column Using a Dictionary in Pandas Data Frame ? Pandas Convert Two Columns to a DictionaryHow To Convert Pandas Dataframe to a Dictionary Default ThumbnailHow to Collapse Multiple Columns in Pandas? Groupby with Dictionary

Filed Under: Pandas 101, Python, Python Tips, Uncategorized Tagged With: Pandas map(), Pandas replace column values

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version