• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:10 mins read
You are currently viewing 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 df.applymap().

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


# Syntax of applymap()
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 passed as keyword 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("Create DataFrame:\n", df)

Yields below output.

pandas apply map

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.

pandas apply map

3. Applying a applymap() to the whole DataFrame

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 the applymap() function. In the example, you see it returns Low for values less than 23000 and “High” for all the values that are more than 23000.

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


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


# 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() function. 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.


 # 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 applymap() 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 an astute software engineer with extensive 3+ years of experience in developing full-stack web applications. My skillets include building backend services for different databases. Mainly I work with Python, Flask, Django, and FastAPIs. Being Python Specialist, I have worked with Numpy, Pandas, and other Python Libraries to build tools that make ETL normalization easier.