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() 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.
- Pandas replace(): How to Replace Multiple Column Values with Dictionary in Python?
- 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.