How to Use the Pandas map() Function?

Learn via video courses
Topics Covered

Pandas map(), as the name suggests, is used to map values with similar series together.

Let us consider a situation where we have a dataset of states in India and their population in alphabetical sequence. Here, on the one hand, we have the index and name of the state, and on the other hand, we have the index and population. Since the index is a typical value, we can directly map the state to the population.

Pandas Map can be used with series, dictionaries, or functions.

panda_map_fun-usage

Syntax of Pandas map()

Let us understand the syntax of Panda's map.

Or

Where,

  • Series.map defines the function where the map function is applied to Series.
  • Self,arg and na_action are the parameters.

Parameters

Based on the above Pandas Map syntax, we have three parameters as discussed -

  • Self is an optional attribute that directs the function to use the defined column or series only.
  • Arg is the argument defining how we map the data, which can be either a function, a dictionary, or a series.
  • Na_action defines how we interpret the missing nan values. In both of the above syntaxes, we have set it to None, which suggests that if the value is null, it will be converted to NaN.

Return Type

The return type for Pandas Map will always be a Series, with the same index as the function calling Series.

Examples

Let us begin by creating a custom dataset.

Output:

  1. Series.map() -

    This function in Pandas Map can be used with a column ie a Series, where we can map new values from existing values in the given series.

    The return type is a series as well.

    Example 1:

    Output:

    Example 2:

    Output:

  2. Passing a dataframe -

    As discussed earlier, the Pandas Map function only applies to Series, Dictionaries, or Functions.

    Thus, when we try to pass a Dataframe we get an error.

    Output:

    Example 1:

    Output -

    pandas_map_data

    Example 2:

    Output:

    panda_datamap_datacopy

    Both of the above examples prove that dataframes cannot be mapped.

  3. Using map() with Dictionary -

    Pandas Map can be used with a dictionary, within or outside the function.

    Example 1:

    Output:

  4. Using a Function with Pandas Map -

    We can use functions with Pandas Maps by applying the format function.

    Output:

  5. Use na_action = 'ignore' -

    Let us use the same example as above using the na_action = 'ignore'.

    Output:

    Here, we can see the difference between the two examples. In the first case, since null values are not ignored, it is treated as all the other values in the series. In the second case, we can see that the function returns a null value instead of formatting it.

Conclusion

  • Pandas Map function helps map values which have the same indices.
  • The accepted values are Series, Dictionaries but not dataframes.
  • The return type is always a series.