Built with Tutify.io

    AIStudyLabs

    Pandas Difference Between map, applymap and apply Methods

    Pandas Difference Between map, applymap and apply Methods

    What is the difference between map(), applymap() and apply() methods in pandas? - In padas, all these methods are used to perform either to modify the DataFrame or Series. pandas map() is a method of Series, applymap() is a method of DataFrame, and apply() is defined in both DataFrame and Series.

    In this pandas article, I will explain the pandas differences between map(), applymap() and apply() methods and their similarities with usages using examples.

    Key Points –

    <a id="h-1-difference-between-map-vs-applymap-vs-apply-methods"></a>

    1. Difference Between map() vs applymap() vs apply() methods

    The main advantage of pandas is to manipulate data (transformations) and apply analytics on the data, all these map(), applymap() and apply() methods are used to modify the data however there are differences between these and their usage are slightly different.

    <a id="h-1-1-pandas-series-map"></a>

    1.1 pandas.Series.map()

    <a id="h-1-2-pandas-dataframe-applymap"></a>

    1.2 pandas.DataFrame.applymap()

    <a id="h-1-3-pandas-series-apply-amp-pandas-dataframe-apply"></a>

    1.3 pandas.Series.apply() & pandas.DataFrame.apply()

    <a id="h-2-quick-examples-of-difference-between-map-applymap-and-apply"></a>

    2. Quick Examples of Difference Between map, applymap and apply

    If you are in a hurry, below are some of the quick examples and usage of map(), applymap() and apply() in pandas DataFrame.

    
    # Below are the quick examples
    
    # Use Series.map() method
    df["Col 1"]=df["Col 1"].map(lambda x: x/100)
    
    # Use Pandas Series.apply() Function to Single Column
    df["A"]=df["A"].apply(lambda x: x/10)
    
    # Use DataFrame.apply() method
    df2 = df.apply(lambda x: x/10)
    
    # Use Pandas DataFrame.applymap() method
    df2 = df.applymap(lambda a: a*10)
    
    # Use DataFrame.applymap() method
    df2 = df.applymap(lambda a: str(a)+".00")
    

    Now, let's create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names A, B, and C.

    
    import pandas as pd
    import numpy as np
    data = [(3,5,7), (2,4,6),(5,8,9)]
    df = pd.DataFrame(data, columns = ['A','B','C'])
    print(df)
    

    Yields below output.

    
    # Output:
       A  B  C
    0  3  5  7
    1  2  4  6
    2  5  8  9
    

    <a id="h-3-use-pandas-series-apply-function-to-single-column"></a>

    3. Use Pandas Series.apply() Function to Single Column

    In order to update a single DataFrame column by applying some transformations use Series.apply() method. On the below example df['A'] returns a Series object.

    
    # Use Pandas DataFrame.apply() Function to Single Column.
    df["A"]=df["A"].apply(lambda x: x/10)
    print(df)
    

    Yields below output.

    
    # Output:
         A  B  C
    0  0.3  5  7
    1  0.2  4  6
    2  0.5  8  9
    

    <a id="h-4-use-dataframe-apply-function-to-each-column"></a>

    4. Use DataFrame.apply() Function to Each Column

    You can also apply a lambda expression, the pandas.DataFrmae.apply() is used to apply a transformation on all columns using lambda expression. The Below example, division 10 to all column values.

    
    # Use DataFrame.apply() method
    df2 = df.apply(lambda x: x/10)
    print(df2)
    

    Yields below output.

    
    # Output:
         A    B    C
    0  0.3  0.5  0.7
    1  0.2  0.4  0.6
    2  0.5  0.8  0.9
    

    <a id="h-5-use-pandas-dataframe-applymap-method"></a>

    5. Use Pandas DataFrame.applymap() Method

    DataFrame.applymap() is another approach to update all DataFrame columns at a time, similar to DataFrame.app() method.

    
    # Use Pandas DataFrame.applymap() method
    df2 = df.applymap(lambda a: a*10)
    print(df2)
    

    Yields below output.

    
    # Output:
        A   B   C
    0  30  50  70
    1  20  40  60
    2  50  80  90
    

    Besides mathematical operations, you can also perform other operations to the elements of the DataFrame. It appends .00 at the end of each element in the DataFrame df.

    
    # Use DataFrame.applymap() method
    df2 = df.applymap(lambda a: str(a)+".00")
    print(df2)
    

    Yields below output.

    
    # Output:
          A     B     C
    0  3.00  5.00  7.00
    1  2.00  4.00  6.00
    2  5.00  8.00  9.00
    

    <a id="h-6-use-series-map-method"></a>

    6. Use Series.map() Method

    You can only use the Series.map() method with the particular column of a DataFrame.

    
    # Use DataFrame.map() Method
    df["Col 1"]=df["Col 1"].map(lambda x: x/100)
    print(df)
    

    Yields below output.

    
    # Output:
       Col 1  Col 2  Col 3
    A    0.2     32     65
    B    0.3     55     78
    C    0.5     85     80
    D    0.7     54     97
    

    <a id="h-7-complete-example-for"></a>

    7. Complete Example For

    
    import pandas as pd
    data = [(3,5,7), (2,4,6),(5,8,9)]
    df = pd.DataFrame(data, columns = ['A','B','C'])
    print(df)
    
    # Use Pandas DataFrameapply() Function to Single Column
    df["A"]=df["A"].apply(lambda x: x/10)
    print(df)
    
    # Use DataFrame.apply() method
    df2 = df.apply(lambda x: x/10)
    print(df2)
    
    # Use Pandas DataFrame.applymap() method
    df2 = df.applymap(lambda a: a*10)
    print(df2)
    
    # Use DataFrame.applymap() method
    df2 = df.applymap(lambda a: str(a)+".00")
    print(df2)
    
    # Use DataFrame.map() Method
    df["Col 1"]=df["Col 1"].map(lambda x: x/100)
    print(df)
    

    <a id="h-frequently-asked-questions-on-difference-between-map-applymap-and-apply"></a>

    Frequently Asked Questions on Difference Between map, applymap and apply

    The main difference lies in their application scope. map is used for element-wise operations on Series objects, applying a function to each element independently. On the other hand, applymap is used for element-wise operations on DataFrame objects, applying a function to each element independently. While both methods operate element-wise, they are applied on different data structures.

    apply is more versatile compared to map and applymap. While map and applymap are primarily used for element-wise operations, apply can handle more complex functions and is suitable for row- or column-wise operations on DataFrame objects. It allows you to apply a function along an axis of the DataFrame, providing flexibility for more intricate data transformations.

    The map method is ideal for simple, element-wise operations on Series objects. If you need to apply a function to each element independently without considering other elements in the Series, map is a suitable choice. It's commonly used when you want to perform a straightforward transformation, such as converting categorical values to numerical ones or scaling values.

    You should use the applymap method when you need to perform element-wise operations on every element in a DataFrame independently. It is particularly useful for applying a function to each cell of the DataFrame without any dependency on other rows or columns. applymap is efficient for applying simple functions across the entire DataFrame, such as scaling or formatting operations.

    The apply method is preferred when you need more flexibility and want to apply functions along the rows or columns of a DataFrame. Unlike map and applymap, apply allows you to apply functions that operate on entire rows or columns, enabling complex data manipulations. It's suitable for scenarios where you need to perform calculations or transformations that involve aggregating values across rows or columns.

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

    Conclusion

    In this article, you have learned the difference between Series.map(), DataFrame.applymap(), Series.apply() and DataFrame.apply() methods of pandas with examples.

    Happy Learning !!

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

    Related Articles

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

    References