Python for Beginners: Pandas Applymap to a Dataframe in Python :

Python for Beginners: Pandas Applymap to a Dataframe in Python
by:
blow post content copied from  Planet Python
click here to view original post


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.

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!

The post Pandas Applymap to a Dataframe in Python appeared first on PythonForBeginners.com.


January 13, 2023 at 07:30PM
Click here for more details...

=============================
The original post is available in Planet Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce