• 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 / How to Replace Multiple Column Values with Dictionary in Python

How to Replace Multiple Column Values with Dictionary in Python

April 27, 2021 by cmdlinetips

Sometimes you might like to change the content of Pandas dataframe, values in one or more columns (not the names of the columns) with some specific values. Pandas’ replace() function is a versatile function to replace the content of a Pandas data frame. First, we will see how to replace multiple column values in a Pandas dataframe using a dictionary, where the key specifies column values that we want to replace and values in the dictionary specifies what we want as shown in the illustration.
Pandas Replace Multiple Column Values with Dictionary
Pandas Replace Multiple Column Values with Dictionary

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 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.

cluster1 = sample(name_list,4)
cluster2 = sample(name_list,4)
cluster3 = sample(name_list,4)

Now, we can use these lists to create a dataframe with 3 columns.

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

Our dataframe look like this.

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

If we want to create a new data dataframe replace the column values of all columns at the same time, we can use Python dictionary to specify how we want to replace each value. In this example our dataframe with multiple columns are made of four values, name1, name2, name3, and name4. With the dictionary we specify the new values and provide the dictionary as input to the replace() function.

df.replace({"name1":"Symbol1",
            "name2":"Symbol2",
            "name3":"Symbol3",
            "name4":"Symbol4"})

Now we get a new dataframe replacing the values of multiple columns at the same time.

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

We can also create dictionary beforehand and use the dictionary to replace multiple columns’ values with Pandas replace() function.

symbol_list = ["symbol1", "symbol2","symbol3","symbol4"]
n2s = dict(zip(name_list,symbol_list))
n2s
{'name1': 'symbol1',
 'name2': 'symbol2',
 'name3': 'symbol3',
 'name4': 'symbol4'}
df.replace(n2s)
	cluster1	cluster2	cluster3
0	symbol1	symbol1	symbol4
1	symbol4	symbol3	symbol1
2	symbol3	symbol4	symbol3
3	symbol2	symbol2	symbol2

Pandas replace(): How to Replace Values of a Specific Column with a Dictionary?

In the above example, we replaced all column’s values at the same time. With replace() we can also specify a column of interest to change its values.

In the example below, we use dictionary and specify a column of interest to change its values.

df.replace({'cluster1': {"name1": "SYMBOL",
                        "name2":"Symbooooo"}})

Note, we have changed first column’s values using the dictionary. Other column’s values’ remain the same.

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

Pandas replace(): How to Replace a Single Value ?

Instead of a dictionary, we can also change a single value in a dataframe to another value. To do that we specify the value to be replaced and the value we want as shown below.

df.replace("name1", "SYMBOL")

In this example, we have changed every instance of “name1” to “SYMBOL”.

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

Pandas replace(): How to Replace Multiple Values with a Single Value?

Pandas replace() function is versatile. We can also provide multiple values we would like to be replaced using a list. In this example, we replace values in a list to a single value.

df.replace(["name1", "name2","name3"], "SYMBOL")

Here, we have changed every instance of name1, name2, and name3 to “SYMBOL”

cluster1	cluster2	cluster3
0	SYMBOL	SYMBOL	name4
1	name4	SYMBOL	SYMBOL
2	SYMBOL	name4	SYMBOL
3	SYMBOL	SYMBOL	SYMBOL

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 Change Multiple Columns Values with mapPandas map: Change Multiple Column Values with a Dictionary Pandas applymap() to change values of a dataframePandas applymap(): Change values of Dataframe Default ThumbnailHow To Add a New Column Using a Dictionary in Pandas Data Frame ? Default ThumbnailHow to Collapse Multiple Columns in Pandas? Groupby with Dictionary

Filed Under: Pandas 101, Pandas replace, Python, Python Tips Tagged With: Pandas101, Python

Reader Interactions

Trackbacks

  1. Pandas map: Change Multiple Column Values with a Dictionary - Python and R Tips says:
    April 30, 2021 at 10:42 pm

    […] us create some data as before using sample from random […]

  2. Pandas applymap(): Change values of Dataframe - Python and R Tips says:
    May 11, 2021 at 8:09 pm

    […] 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. […]

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