Pandas apply map (applymap()) Explained

pandas.DataFrame.applymap() is a built-in function used to apply() and map() functions together on DataFrame element-wise. The first function is the pandas.DataFrame.apply() and the second function that we will discuss today is pandas apply map.

pandas.DataFrame.applymap() function is implemented via apply() with a little wrap-up over the passed function parameter. The df.applymap() function is applied to the element of a dataframe one element at a time. This means that it takes the separate cell value as a parameter and assigns the result back to this cell.

We also have pandas.DataFrame.apply() method which takes the whole column as a parameter. It then assigns the result to this column. DataFrame.apply() can also take additional arguments element-wise for a single column. While the DataFrame.applymap() function can apply a function to several columns.

Keynotes about pandas apply map function:

  • applymap is defined on DataFrames ONLY
  • applymap is elementwise for DataFrames
  • applymap is good for elementwise transformations across multiple rows/columns
  • applymap slightly faster than apply in some cases.

1. pandas.DataFrame.applymap() Syntax and Usage

Below is the syntax of the DataFrame.applymap() function, this function takes one required parameter, one optional parameter and **kwargs . We will explain them one by one. The DataFrame.applymap() returns a DataFrame object, specifically saying a Transformed DataFrame.


DataFrame.applymap(func, na_action=None, **kwargs)[source]
  • func — Python function, returns a single value from a single value.
  • na_action — Default value is None, if the value is ‘ignore’ then propagate NaN values, without passing it to function.
  • **kwargs — With this parameter, additional keyword arguments are pass as keywords arguments to func.

In order to explain several examples of how to perform group, first, let’s create a simple DataFrame with a combination of string and numeric columns.


import pandas as pd
technologies   = ({
    'Courses':["Spark","PySpark","Hadoop","Python","Pandas","Hadoop","Spark","Python","NA"],
    'Fee' :[22000,25000,23000,24000,26000,25000,25000,22000,1500],
    'Duration':['30days','50days','55days','40days','60days','35days','30days','50days','40days'],
    'Discount':[1000,2300,1000,1200,2500,None,1400,1600,0]
          })
df = pd.DataFrame(technologies)
print(df)

Yields below output.

   Courses    Fee Duration  Discount
0    Spark  22000   30days    1000.0
1  PySpark  25000   50days    2300.0
2   Hadoop  23000   55days    1000.0
3   Python  24000   40days    1200.0
4   Pandas  26000   60days    2500.0
5   Hadoop  25000   35days       NaN
6    Spark  25000   30days    1400.0
7   Python  22000   50days    1600.0
8       NA   1500   40days       0.0

2. Pandas apply map Example

We have now an idea of the syntax of the applymap() function. This method is used to apply a function elementwise. In the following example, we have used the df.applymap() function to add an extra 500 amount to the “Fee” column.


# convert the fee column to a dataframe
t_df = pd.DataFrame(df['Fee'])
# added 300 to each item in a fee element
t_df.applymap(lambda x:x+300)

Yields the following output. Each cell in the “Fee” column has now increased by 300 amount.

      Fee   
0	22300
1	25300
2	23300
3	24300
4	26300
5	25300
6	25300
7	22300
8	1800

3. Applying a function to the whole DataFrame using apply map

We will take a subset of our complete dataframe and will include only numerical values so that you can see how we are going to apply a function to a Complete DataFrame.

we have created a dataframe with two columns from the existing DataFrame just to check how we can pass arguments to applymap() function. In the example, you see it returns Low for values less than 23000 and “High” for all the values which are more than 23000.

You can see the df.applymap() is used to apply the function group_money() to all cells of the dataFrame.


# create a function 
def group_money(money, threshold):
    if (money>threshold):
        return 'High'
    else:
        return 'Low'

# create a dataframe form numerical columns
t_df = pd.DataFrame(df[['Fee', 'Discount']])
# Apply the function and pass the threshold value
t_df.applymap(group_money,threshold=23000)

Yields the following output.

 	Fee	Discount
0	Low	Low
1	High	Low
2	Low	Low
3	High	Low
4	High	Low
5	High	Low
6	High	Low
7	Low	Low
8	Low	Low

4. Pandas df.applymap() Ignore NaN value

The “na_ignore” parameter of df.applymap() function is extremely important in cases if your dataFrame contains “NaN” values. This optional parameter lets the function know that ignores the “NaN” values is the value of the “na_action” is set to “ignore”.

See the following example, we have only one NaN value and the applymap function is not applied to that cell.


# create a dataframe form numerical columns
t_df = pd.DataFrame(df[['Fee', 'Discount']])
# Apply the function and pass the threshold value
t_df.applymap(group_money,na_action='ignore',threshold=23000)

5. The Difference between df.applymap() vs df.apply() vs df.map()

The very basic level difference between the pandas apply map , apply() and map() function is that, applymap is defined on DataFrames, map() is defined on Series while df.apply() work on both.

Look at the following table explaining the key difference between the three of them.

Pandas applymap()pandas apply()pandas map()
Defined on Series?NoYesYes
Defined on DataFrame?YesYesNo
Elementwise?YesYesYes
Aggregation?NoYesNo
Return?DataFrameSeries, DataFrameSeries

6. Find how many Datatypes you have in a DataFrame

This can be a very good case of using df.applymap() funciton. Suppose we want to find out how many dataypes we have and which column has which type of data.

See the following example, where we use the pandas apply map function to find out the data type of each column.


# find the data type of each column
df.applymap(type).value_counts()

Yields, the following output.

 Courses        Fee            Duration       Discount       
class 'str'  class 'int'  class 'str'  class 'float'    9
dtype: int64

7. Conclusion

In this article, you have learned about pandas apply map function syntax, usage, and examples. We have also discussed the difference between the apply(), map() and applymap() functions. If you still have any questions in mind please feel free to leave them in the comments.

AlixaProDev

I am a software Engineer with extensive 4+ years of experience in Programming related content Creation.

Leave a Reply

You are currently viewing Pandas apply map (applymap()) Explained