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

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Return Type

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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.