Built with Tutify.io

    AIStudyLabs

    Pandas apply map (applymap()) Explained

    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:

    <a id="h-1-pandas-dataframe-applymap-syntax-and-usage"></a>

    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]
    

    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.

    <a id="h-2-pandas-apply-map-example"></a>

    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.

    <a id="h-3-applying-a-applymap-to-the-whole-dataframe"></a>

    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
    

    <a id="h-4-pandas-df-applymap-ignore-nan-value"></a>

    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 theapplymap 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)
    

    <a id="h-5-the-difference-between-df-applymap-vs-df-apply-vs-df-map"></a>

    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? | No | Yes | Yes | | Defined on DataFrame? | Yes | Yes | No | | Elementwise? | Yes | Yes | Yes | | Aggregation? | No | Yes | No | | Return? | DataFrame | Series, DataFrame | Series |

    <a id="h-6-find-how-many-datatypes-you-have-in-a-dataframe"></a>

    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
    

    <a id="h-faq-on-pandas-apply-map-applymap"></a>

    FAQ on Pandas apply map (applymap())

    applymap() is used to apply a function elementwise (i.e., to each individual element) across all cells of a DataFrame.

    apply() works along a specific axis (rows or columns) in a DataFrame or Series, whereas applymap() applies a function to each element in a DataFrame individually.

    You can pass any function that takes a single element and returns a transformed value. You can also use lambda functions.

    You can perform elementwise transformations such as mathematical operations, string manipulations, type conversions, or conditional logic on DataFrame elements.

    You can apply a function that operates on specific data types, such as converting all strings to lowercase or applying rounding to numeric values.

    <a id="h-conclusion"></a>

    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() andapplymap() functions. If you still have any questions in mind please feel free to leave them in the comments.

    <a id="h-related-articles"></a>

    Related Articles