upby.html)
  * [ Resampling ](../resampling.html)
  * [ Style ](../style.html)
  * [ Plotting ](../plotting.html)
  * [ Options and settings ](../options.html)
  * [ Extensions ](../extensions.html)
  * [ Testing ](../testing.html)
  * [ Missing values ](../missing_value.html)

  * [ __ ](../../index.html)
  * [ API reference ](../index.html)
  * [ DataFrame ](../frame.html)
  * pandas.DataFrame.map 

#  pandas.DataFrame.map  #

DataFrame.  map  (  _ func  _ , _ na_action  =  None  _ , _ **  kwargs  _ )  [
[source]  ](https://github.com/pandas-
dev/pandas/blob/v2.2.2/pandas/core/frame.py#L10376-L10468) #

    

Apply a function to a Dataframe elementwise.

New in version 2.1.0:  DataFrame.applymap was deprecated and renamed to
DataFrame.map.

This method applies a function that accepts and returns a scalar to every
element of a DataFrame.

Parameters  :

    

**func** callable

    

Python function, returns a single value from a single value.

**na_action** {None, âignoreâ}, default None

    

If âignoreâ, propagate NaN values, without passing them to func.

****kwargs**

    

Additional keyword arguments to pass as keywords arguments to  func  .

Returns  :

    

DataFrame

    

Transformed DataFrame.

See also

[ ` DataFrame.apply  ` ](pandas.DataFrame.apply.html#pandas.DataFrame.apply
"pandas.DataFrame.apply")

    

Apply a function along input axis of DataFrame.

[ ` DataFrame.replace  `
](pandas.DataFrame.replace.html#pandas.DataFrame.replace
"pandas.DataFrame.replace")

    

Replace values given in  to_replace  with  value  .

[ ` Series.map  ` ](pandas.Series.map.html#pandas.Series.map
"pandas.Series.map")

    

Apply a function elementwise on a Series.

Examples

    
    
    >>> df = pd.DataFrame([[1, 2.12], [3.356, 4.567]])
    >>> df
           0      1
    0  1.000  2.120
    1  3.356  4.567
    
    
    
    >>> df.map(lambda x: len(str(x)))
       0  1
    0  3  4
    1  5  5
    

Like Series.map, NA values can be ignored:

    
    
    >>> df_copy = df.copy()
    >>> df_copy.iloc[0, 0] = pd.NA
    >>> df_copy.map(lambda x: len(str(x)), na_action='ignore')
         0  1
    0  NaN  4
    1  5.0  5
    

It is also possible to use  map  with functions that are not  lambda
functions:

    
    
    >>> df.map(round, ndigits=1)
         0    1
    0  1.0  2.1
    1  3.4  4.6
    

Note that a vectorized version of  func  often exists, which will be much
faster. You could square each number elementwise.

    
    
    >>> df.map(lambda x: x**2)
               0          1
    0   1.000000   4.494400
    1  11.262736  20.857489
    

But itâs better to avoid map in that case.

    
    
    >>> df ** 2
               0          1
    0   1.000000   4.494400
    1  11.262736  20.857489
    

[ __ previous  pandas.DataFrame.apply  ](pandas.DataFrame.apply.html "previous
page") [ next  pandas.DataFrame.applymap  __ ](pandas.DataFrame.applymap.html
"next page")

__ On this page

  * ` DataFrame.map()  `

[ __ Show Source ](../../_sources/reference/api/pandas.DataFrame.map.rst.txt)

© 2024, pandas via [ NumFOCUS, Inc. ](https://numfocus.org) Hosted by [
OVHcloud ](https://www.ovhcloud.com) .

Created using [ Sphinx ](https://www.sphinx-doc.org/) 7.2.6.  

Built with the [ PyData Sphinx Theme ](https://pydata-sphinx-
theme.readthedocs.io/en/stable/index.html) 0.14.4.