Skip to main content
`Ctrl`+`K`

# pandas.Series.map

Series.map(func=None, na_action=None, engine=None, **kwargs)

  Map values of Series according to an input mapping or function.

  Used for substituting each value in a Series with another value, that may be derived from a function, a `dict` or a `Series`.

  Parameters:
    func function, collections.abc.Mapping subclass or Series

      Function or mapping correspondence.

    na_action{None, ‘ignore’}, default None

      If ‘ignore’, propagate NaN values, without passing them to the mapping correspondence.

    engine decorator, optional

      Choose the execution engine to use to run the function. Only used for functions. If `map` is called with a mapping or `Series`, an exception will be raised. If `engine` is not provided the function will be executed by the regular Python interpreter.

      Options include JIT compilers such as Numba, Bodo or Blosc2, which in some cases can speed up the execution. To use an executor you can provide the decorators `numba.jit`, `numba.njit`, `bodo.jit` or `blosc2.jit`. You can also provide the decorator with parameters, like `numba.jit(nogit=True)`.

      Not all functions can be executed with all execution engines. In general, JIT compilers will require type stability in the function (no variable should change data type during the execution). And not all pandas and NumPy APIs are supported. Check the engine documentation for limitations.

      Added in version 3.0.0.

    **kwargs

      Additional keyword arguments to pass as keywords arguments to arg.

      Added in version 3.0.0.

  Returns:
    Series

      Same index as caller.

  See also

  `Series.apply`

    For applying more complex functions on a Series.

  `Series.replace`

    Replace values given in to_replace with value.

  `DataFrame.apply`

    Apply a function row-/column-wise.

  `DataFrame.map`

    Apply a function elementwise on a whole DataFrame.

  Notes

  When `arg` is a dictionary, values in Series that are not in the dictionary (as keys) are converted to `NaN`. However, if the dictionary is a `dict` subclass that defines `__missing__` (i.e. provides a method for default values), then this default is used rather than `NaN`.

  Examples

      >>> s = pd.Series(["cat", "dog", np.nan, "rabbit"])
      >>> s
      0      cat
      1      dog
      2      NaN
      3   rabbit
      dtype: str

  `map` accepts a `dict` or a `Series`. Values that are not found in the `dict` are converted to `NaN`, unless the dict has a default value (e.g. `defaultdict`):

      >>> s.map({"cat": "kitten", "dog": "puppy"})
      0   kitten
      1    puppy
      2      NaN
      3      NaN
      dtype: str

  It also accepts a function:

      >>> s.map("I am a {}".format)
      0       I am a cat
      1       I am a dog
      2       I am a nan
      3    I am a rabbit
      dtype: str

  To avoid applying the function to missing values (and keep them as `NaN`) `na_action='ignore'` can be used:

      >>> s.map("I am a {}".format, na_action="ignore")
      0     I am a cat
      1     I am a dog
      2            NaN
      3  I am a rabbit
      dtype: str

  For categorical data, the function is only applied to the categories:

      >>> s = pd.Series(list("cabaa"))
      >>> s.map(print)
      c
      a
      b
      a
      a
      0    None
      1    None
      2    None
      3    None
      4    None
      dtype: object

      >>> s_cat = s.astype("category")
      >>> s_cat.map(print)  # function called once per unique category
      a
      b
      c
      0    None
      1    None
      2    None
      3    None
      4    None
      dtype: object