Pandas Difference Between map, applymap and apply Methods

  • Post author:
  • Post category:Pandas
  • Post last modified:February 12, 2024
  • Reading time:19 mins read

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 –

  • map() is used for element-wise operations on Series objects, applying a function to each element independently.
  • applymap() is used for element-wise operations on DataFrame objects, applying a function to each element independently.
  • apply() is used for row- or column-wise operations on DataFrame objects, applying a function along an axis of the DataFrame.
  • map() and applymap() are primarily used for element-wise operations, while apply is more versatile, allowing operations along rows or columns of a DataFrame.
  • map() is typically used with Series objects, applymap() with DataFrames, and apply() can be used with both, offering different levels of granularity and flexibility in function application.

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.

1.1 pandas.Series.map()

  • This method defined only in Series and not present in DataFrame.
  • map() accepts dicts, Series, or callable
  • You can use this to perform operations on a specific column of a DataFrame as each column in a DataFrame is Series.
  • map() when passed a dictionary/Series will map elements based on the keys in that dictionary/Series. Missing values will be recorded as NaN in the output.
  • Series.map() operate on one element at time

1.2 pandas.DataFrame.applymap()

  • Pandas DataFrame.applymap() method is defined only in DataFrame.
  • Accept callables only.
  • applymap() is elementwise for DataFrames.
  • applymap() performs better than apply().
  • applymap() operates on one element at time

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

  • This method defined in both Series and DataFrame
  • Accept callables only
  • apply() also works elementwise but is suited to more complex operations and aggregation.
  • DataFrame.apply() operates on entire rows or columns at a time.
  • Series.apply() operate on one element at time

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

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

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

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

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

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)

Frequently Asked Questions on Difference Between map, applymap and apply

What is the main difference between the map and applymap methods in Pandas?

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.

How does the apply method differ from map and applymap in Pandas?

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.

When should I use the map method in Pandas?

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.

In what scenarios should I use the applymap method in Pandas?

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.

When is the apply method preferred over map and applymap in Pandas?

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.

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 !!

References

Naveen (NNK)

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium

Leave a Reply