• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Pandas Applymap to a Dataframe in Python

Pandas Applymap to a Dataframe in Python

Author: Aditya Raj
Last Updated: January 13, 2023

The pandas applymap() method is used to apply a function to a dataframe element-wise. This article will discuss different ways to use the applymap method with a pandas dataframe.

Table of Contents
  1. The applymap() Method
  2. Pandas Applymap to a Dataframe
  3. Pandas Applymap With Specific Columns in a Dataframe
  4. Pandas Applymap Ignore NaN Values
  5. Pandas Applymap With a Lambda Function in Python
  6. Conclusion

The applymap() Method

The applymap() method is used to apply a function element-wise to a pandas dataframe. It has the following syntax.

DataFrame.applymap(func, na_action=None, **kwargs)

Here,

  • The func parameter takes an input function. The function should take an element of the dataframe as input and return a value.
  • The na_action parameter is used to handle NaN values in the input dataframe. If the na_action parameter is set to “ignore”, the NaN values are not passed to the function in the func parameter and are directly propagated to the output dataframe. Otherwise, None is given as input to the function.
  • After execution, the applymap() method returns the transformed dataframe.

Pandas Applymap to a Dataframe

To perform an operation on the values in a dataframe, you can use the applymap() method as shown in the following example.

import pandas as pd
import numpy as np
def fun1(x):
    nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
    if x in nameDict:
        return nameDict[x]
    else:
        return x
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newDf=df.applymap(fun1)
print("The updated dataframe is:")
print(newDf)

Output:

The input dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The updated dataframe is:
   Roll    Maths  Physics Chemistry
0     1  Hundred   Eighty    Ninety
1     2   Eighty  Hundred    Ninety
2     3   Ninety   Eighty   Seventy
3     4  Hundred  Hundred    Ninety
4     5   Ninety   Ninety    Eighty
5     6   Eighty  Seventy   Seventy

In the above example, we have defined the function func1 that takes a number as input and returns its word representation. Then, we created a dataframe from a list of dictionaries. Next, we invoked the applymap() method on the dataframe with func1 as its input argument. After execution, you can observe that we get the desired output.

Pandas Applymap With Specific Columns in a Dataframe

Instead of the entire dataframe, you can also choose to use the pandas applymap method on only a few columns. For this, you can first select the desired columns in the dataframe on whose values you need to apply any function using python indexing. Next, you can invoke the applymap() method on the selected columns as shown below.

import pandas as pd
import numpy as np
def fun1(x):
    nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
    if x in nameDict:
        return nameDict[x]
    else:
        return x
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
df[["Maths","Physics", "Chemistry"]]=df[["Maths","Physics", "Chemistry"]].applymap(fun1)
print("The updated dataframe is:")
print(df)

Output:

The input dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The updated dataframe is:
   Roll    Maths  Physics Chemistry
0     1  Hundred   Eighty    Ninety
1     2   Eighty  Hundred    Ninety
2     3   Ninety   Eighty   Seventy
3     4  Hundred  Hundred    Ninety
4     5   Ninety   Ninety    Eighty
5     6   Eighty  Seventy   Seventy

In this example, we have first selected the Maths, Physics, and Chemistry column using indexing operator. Then, we used the applymap() method to apply the function fun1 to the selected columns.

Pandas Applymap Ignore NaN Values

If the function passed to the applymap() method is not able to handle NaN values, the program will run into an error if there are NaN values present in the data frame. You can observe this in the following example.

import pandas as pd
import numpy as np
def fun1(x):
    nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
    if x in nameDict:
        return nameDict[x]
    else:
        return int(x)
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":np.nan, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":np.nan, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": np.nan}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
df[["Maths","Physics", "Chemistry"]]=df[["Maths","Physics", "Chemistry"]].applymap(fun1)
print("The updated dataframe is:")
print(df)

Output:

ValueError: cannot convert float NaN to integer

In the above example, the function fun1() cannot handle NaN values. Hence, the program runs into the ValueError exception.

To ignore NaN values while applying a function to a dataframe using the applymap() method, you can set the na_action parameter to “ignore” as shown below.

import pandas as pd
import numpy as np
def fun1(x):
    nameDict={100:"Hundred", 90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty"}
    if x in nameDict:
        return nameDict[x]
    else:
        return int(x)
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":np.nan, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":np.nan, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": np.nan}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
df[["Maths","Physics", "Chemistry"]]=df[["Maths","Physics", "Chemistry"]].applymap(fun1,na_action="ignore")
print("The updated dataframe is:")
print(df)

Output:

The input dataframe is:
   Roll  Maths  Physics  Chemistry
0     1  100.0     80.0       90.0
1     2    NaN    100.0       90.0
2     3   90.0     80.0       70.0
3     4  100.0      NaN       90.0
4     5   90.0     90.0       80.0
5     6   80.0     70.0        NaN
The updated dataframe is:
   Roll    Maths  Physics Chemistry
0     1  Hundred   Eighty    Ninety
1     2      NaN  Hundred    Ninety
2     3   Ninety   Eighty   Seventy
3     4  Hundred      NaN    Ninety
4     5   Ninety   Ninety    Eighty
5     6   Eighty  Seventy       NaN

In this example, we have set the na_action parameter to "ignore". Due to this, fun1() is not applied on the NaN values and NaN values are directly copied into output dataframe.

Pandas Applymap With a Lambda Function in Python

If the function that you want to apply to the dataframe values is a single line statement, you can use a lambda function with the applymap() method as shown below.

import pandas as pd
import numpy as np
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 85}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
df[["Maths","Physics", "Chemistry"]]=df[["Maths","Physics", "Chemistry"]].applymap(lambda x: x//10)
print("The updated dataframe is:")
print(df)

Output:

The input dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2    100      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         85
The updated dataframe is:
   Roll  Maths  Physics  Chemistry
0     1     10        8          9
1     2     10       10          9
2     3      9        8          7
3     4     10       10          9
4     5      9        9          8
5     6      8        7          8

Conclusion

In this article, we have discussed different ways to use the pandas applymap() method on a dataframe. We also discussed how to use the applymap() method with lambda function in python.

I hope you enjoyed reading this article.

To learn more about python programming, you can read this article on how to sort a pandas dataframe. You might also like this article on how to drop columns from a pandas dataframe.

Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us