• 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 / Python / Pandas applymap(): Change values of Dataframe

Pandas applymap(): Change values of Dataframe

May 11, 2021 by cmdlinetips

Pandas applymap() is yet another useful function to change the content of a dataframe. In this tutorial, we will learn how to use Pandas applymap() 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. And then we also saw we can do similar task using Pandas map() function as well. As we all know, there are multiple solutions to a problem.

Pandas applymap() to change values of a dataframe
Pandas applymap() to change values of a dataframe

Pandas applymap() function takes in Pandas data frame as input and apply a user defined function to change content of the data frame element-wise. To change values of a data frame, we can write a lambda function with dictionary that returns a new value for the elements in data frame.

Let us use the same example that we used for Pandas replace() and map() functions to replace values of a data frame with a dictionary.

import pandas as pd
# import random 
from random import sample

We create some sample data using sample() function in random module.


# Create two lists in Python
name_list = ["name1", "name2","name3","name4"]
cluster1 = sample(name_list,4)
cluster2 = sample(name_list,4)
cluster3 = sample(name_list,4)

Let us create a data frame three columns with string values.


df = pd.DataFrame({"cluster1":cluster1,
              "cluster2":cluster2,
              "cluster3":cluster3,
             })
df
cluster1	cluster2	cluster3
0	name1	name4	name3
1	name4	name1	name1
2	name2	name3	name4
3	name3	name2	name2

We want to change the values of the dataframe with some other values. Here, we create a dictionary using the old values that we want to change as keys and the new values as dictionary values.

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

And our dictionary looks like.


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

Now we can use Pandas applymap() function to change values element wise. We provide lambda function as input to applymap() function, with input to lambda function is element and the output is the result querying the key with dictionary.

df.applymap(lambda x: n2s[x])

And we get a new dataframe with replaced values as output.

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

As I said before, this is not the only way to replace the content of a Pandas dataframe. Check out the other two ways to change the values in Pandas.

  1. Pandas replace(): How to Replace Multiple Column Values with Dictionary in Python?
  2. Pandas map: Change Multiple Column Values with a Dictionary

It will be interesting to compare the running times of the three Pandas functions to change a dataframe’s content, but that is for another time.

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 Pandas Change Multiple Columns Values with mapPandas map: Change Multiple Column Values with a Dictionary Default ThumbnailHow to Change Type for One or More Columns in Pandas Dataframe? Pandas Convert Two Columns to a DictionaryHow To Convert Pandas Dataframe to a Dictionary

Filed Under: Pandas 101, Pandas applymap, Python, Python Tips Tagged With: Python

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