๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-pandas-map
Python | pandas.map() - GeeksforGeeks
August 23, 2023 - Pandas is a widely used library for manipulating datasets. There are various in-built functions of pandas, one such function is pandas.map(), which is used to map values from two series having one similar column.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ pandas-map
Pandas map() Function | Methods and Examples
September 21, 2023 - This function allows you to apply a transformation or mapping function to each element of a DataFrame, resulting in a new DataFrame with the modified values. Before delving into the details, let's explore the basic syntax of the pandas map function:
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ pandas
pandas: Apply functions to values, rows, columns with map(), apply() | note.nkmk.me
January 17, 2024 - To apply a function to a specific row or column, extract the row or column as a Series and use the map() or apply() methods of Series. pandas: Select rows/columns by index (numbers and names)
๐ŸŒ
Medium
medium.com โ€บ @amit25173 โ€บ pandas-map-vs-apply-practical-guide-51f046a15cd9
pandas map vs apply (Practical Guide) | by Amit Yadav | Medium
March 6, 2025 - Think of map() as a personal assistant for a Pandas Seriesโ€”it goes through each value one by one and applies a function.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas map() function โ€“ examples
pandas map() Function - Examples - Spark By {Examples}
March 27, 2024 - pandas map() function from Series is used to substitute each value in a Series with another value, that may be derived from a function,
Find elsewhere
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Index.map.html
pandas.Index.map โ€” pandas 3.0.2 documentation
>>> idx = pd.Index([1, 2, 3]) >>> idx.map({1: "a", 2: "b", 3: "c"}) Index(['a', 'b', 'c'], dtype='str')
Top answer
1 of 12
755

apply works on a row / column basis of a DataFrame
applymap works element-wise on a DataFrame
map works element-wise on a Series


Straight from Wes McKinney's Python for Data Analysis book, pg. 132 (I highly recommended this book):

Another frequent operation is applying a function on 1D arrays to each column or row. DataFrameโ€™s apply method does exactly this:

In [116]: frame = DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon'])

In [117]: frame
Out[117]: 
               b         d         e
Utah   -0.029638  1.081563  1.280300
Ohio    0.647747  0.831136 -1.549481
Texas   0.513416 -0.884417  0.195343
Oregon -0.485454 -0.477388 -0.309548

In [118]: f = lambda x: x.max() - x.min()

In [119]: frame.apply(f)
Out[119]: 
b    1.133201
d    1.965980
e    2.829781
dtype: float64

Many of the most common array statistics (like sum and mean) are DataFrame methods, so using apply is not necessary.

Element-wise Python functions can be used, too. Suppose you wanted to compute a formatted string from each floating point value in frame. You can do this with applymap:

In [120]: format = lambda x: '%.2f' % x

In [121]: frame.applymap(format)
Out[121]: 
            b      d      e
Utah    -0.03   1.08   1.28
Ohio     0.65   0.83  -1.55
Texas    0.51  -0.88   0.20
Oregon  -0.49  -0.48  -0.31

The reason for the name applymap is that Series has a map method for applying an element-wise function:

In [122]: frame['e'].map(format)
Out[122]: 
Utah       1.28
Ohio      -1.55
Texas      0.20
Oregon    -0.31
Name: e, dtype: object
2 of 12
373

Comparing map, applymap and apply: Context Matters

The major differences are:

Definition

  • map is defined on Series only
  • applymap is defined on DataFrames only
  • apply is defined on both

Input argument

  • map accepts dict, Series, or callable
  • applymap and apply accept callable only

Behavior

  • map is elementwise for Series
  • applymap is elementwise for DataFrames
  • apply also works elementwise but is suited to more complex operations and aggregation. The behaviour and return value depends on the function.

Use case (the most important difference)

  • map is meant for mapping values from one domain to another, so is optimised for performance, e.g.,

    df['A'].map({1:'a', 2:'b', 3:'c'})
    
  • applymap is good for elementwise transformations across multiple rows/columns, e.g.,

    df[['A', 'B', 'C']].applymap(str.strip)
    
  • apply is for applying any function that cannot be vectorised, e.g.,

    df['sentences'].apply(nltk.sent_tokenize)
    

Also see When should I (not) want to use pandas apply() in my code? for a writeup I made a while back on the most appropriate scenarios for using apply. (Note that there aren't many, but there are a fewโ€” apply is generally slow.)


Summarising

map applymap apply
Defined on Series? Yes No Yes
Defined on DataFrame? No Yes Yes
Argument dict, Series, or callable1 callable2 callable
Elementwise? Yes Yes Yes
Aggregation? No No Yes
Use Case Transformation/mapping3 Transformation More complex functions
Returns Series DataFrame scalar, Series, or DataFrame4

Footnotes

  1. 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.

  2. applymap in more recent versions has been optimised for some operations. You will find applymap slightly faster than apply in some cases. My suggestion is to test them both and use whatever works better.

  3. map is optimised for elementwise mappings and transformation. Operations that involve dictionaries or Series will enable pandas to use faster code paths for better performance.

  4. Series.apply returns a scalar for aggregating operations, Series otherwise. Similarly for DataFrame.apply. Note that apply also has fastpaths when called with certain NumPy functions such as mean, sum, etc.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ pandas map function help
r/learnpython on Reddit: Pandas map function help
October 7, 2022 -

I've been doing a Kaggle course about pandas and found a line I don't really understand, so I was hoping someone could help me out a bit.

The line would be this:

n_trop = reviews.description.map(lambda desc: "tropical" in desc).sum()

It wants to count the number of times 'tropical' appears in the description column of a table.

What does 'desc' stand for? Is it description? In that case, can I shorten column names in pandas whenever I feel like it?

I believe I may have a problem with lambdas but I'm quite lost here.

๐ŸŒ
KDnuggets
kdnuggets.com โ€บ how-to-dataframe-map-element-wise-operations-pandas
How to Use dataframe.map() for Element-wise Operations in Pandas - KDnuggets
January 8, 2025 - Recall the syntax of dataframe.map() I showed earlier, which includes the na_action parameter. This parameter allows you to control how missing values are handled. Let me help you understand this with an example. Suppose we are running a grocery store and some prices are missing. In this case, we want to display "Unavailable" instead of NaN. You can do so as follows; import pandas as pd import numpy as np # Sample DataFrame of Grocery Store with some NaN values for price df = pd.DataFrame({ 'Product': ['Apple', 'Banana', 'Cherry', 'Date'], 'Price': [1.2, np.nan, 2.5, np.nan] }) # Mapping funct
๐ŸŒ
Pandas
pandas.pydata.org โ€บ pandas-docs โ€บ version โ€บ 0.19 โ€บ generated โ€บ pandas.Series.map.html
pandas.Series.map โ€” pandas 0.19.2 documentation
>>> s2 = s.map(lambda x: 'this is a string {}'.format(x), na_action=None) 0 this is a string 1.0 1 this is a string 2.0 2 this is a string 3.0 3 this is a string nan dtype: object
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ pandas โ€บ methods โ€บ map
Pandas map()
The map() method returns a Series where each element is the result of applying the mapping argument to each element of the original Series. import pandas as pd # create a Series with fruit names fruits = pd.Series(['apple', 'banana', 'cherry', 'date']) # dictionary mapping fruit names to their ...
๐ŸŒ
ProjectPro
projectpro.io โ€บ recipes โ€บ map-values-in-pandas-dataframe
How to map values in a Pandas DataFrame? -
September 1, 2023 - We sometimes use Python Pandas to map values to other values in Python, i.e., values of a feature with values of another feature.
๐ŸŒ
w3resource
w3resource.com โ€บ pandas โ€บ series โ€บ series-map.php
Pandas: Series - map() function - w3resource
September 15, 2022 - import numpy as np import pandas as pd s = pd.Series(['fox', 'cow', np.nan, 'dog']) s.map('I am a {}'.format) s.map('I am a {}'.format, na_action='ignore')
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To Use map() In Pandas (Python) - YouTube
โ†“ Code Available Below! โ†“ This video shows how to map functions to columns of pandas data frames using .map(). The .map() function operates on pandas serie...
Published ย  November 11, 2020