abcbloggingworld

## Pandas applymap(): Change values of Dataframe

Data is an essential part of any analysis. Data is taken from the real world. So any analysis is incomplete without data. Data can be qualitative as well as quantitative. We can use data to find answers as well as make predictions. There are many kinds of data. Some of them are: * Categorical – eg: Male-Female * Ordinal – eg: Rich-Poor * Interval – eg: Temperature * Ratio – eg: Height, Weight, Age There are many kinds of data. Some of them are: * Categorical – eg: Male-Female * Ordinal – eg: Rich-Poor * Interval – eg: Temperature * Ratio – eg: Height, Weight, Age You can use data to create the

By now, if you have been around the Python community for a while, you have probably heard people raving about the Pandas library. If you haven’t, then you are probably wondering what the big deal is. Well, the Pandas library is a Python library used for data science and data analysis. It is built upon the Numpy library and therefore relies heavily on Numpy. Once you get the basics of Pandas down, you can start to manipulate your data in new and exciting ways. One way to do this is to use the applymap method.

Panda’s applymap() function is another useful function for modifying the contents of a data frame. In this lesson, we will learn how to use the pandas applymap() function to replace the values of multiple columns using a dictionary. Earlier we saw how to use Panda’s replace() function to change the values of multiple columns using a dictionary. We have also seen that a similar task can be performed with the pandas map() function. As we all know, there are several solutions to this problem.

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

Let’s use the same example as for Panda’s replace() and map() functions to replace the values of the data frames with a dictionary.

import pandas as pd
# import random
from random import sample

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

#
name_list = [name1, name2, name3, name4]
cluster1 = sample(name_list,4)
cluster2 = sample(name_list,4)
cluster3 = sample(name_list,4)

Let’s create a data frame with three columns containing character strings.

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 replace the values in the data framework with other values. Here we create a dictionary, with the old values 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 vocabulary is as follows.

{NAME1}: Symbol1,
Name2 : Symbol2,
Name3 : Symbol3,
Name4 : Symbol4′}

We can now use the pandas applymap() function to change the values of one element at a time. We give a lambda function as input to applymap(), where the input of the lambda function is the element and the output is the result of a dictionary key query.

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

And in the output we get a new data frame with the replaced values.

cluster1 cluster2 cluster3
0 symbol1 symbol4 symbol3
1 symbol4 symbol1
2 symbol2 symbol3 symbol4
3 symbol3 symbol2

As I said, this is not the only way to replace the contents of a Pandas data frame. See two other ways to change values in Pandas.

1. Pandas replace() : How to replace the values of different columns by a dictionary in Python ?
2. Panda card: Changing the values of various columns using the dictionary

It will be interesting to compare the execution times of the three Pandas functions for modifying the contents of a data frame, but that’s for another time.

Would you like to make better use of Pandas to work with your data? Check out the Byte Sized Pandas 101 tutorials.

The post Pandas applymap(): The post Dataframe value changes appeared first on .

This source has been very much helpful in doing our research. Read more about pandas applymap multiple arguments and let us know what you think.

#### Related Tags:

pandas applymap multiple columnspandas style applymappandas apply vs applymappandas applymap multiple argumentspandas applymap to certain columnspandas applymap dictionary,People also search for,Privacy settings,How Search works,pandas applymap multiple columns,pandas style applymap,pandas apply vs applymap,pandas applymap multiple arguments,pandas applymap to certain columns,pandas applymap dictionary,pandas dataframe apply function with arguments,pandas dataframe map column values