From the course: Data Analysis with Python and Pandas

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

The map() method

The map() method

- [Instructor] Okay, so let's talk about a function that many of you may never use, but it can be very handy in certain situations. The map method maps values to a column or to an entire data frame. We can pass a dictionary with existing values as the keys and new values as the values of that dictionary. Let's take a look. So here I've created a mapping dictionary. We're taking some of our product families and mapping them to dietary categories. So we have dairy, we're mapping that to non-vegan. Vegetables is being mapped to vegan and fruits is being mapped to vegan. I'm then creating a column based off of these mapped values that's called vegan question mark. And this is going to be based off of our product column. We're then going to map our mapping dictionary and return our vegan column. So let's take a look. Our keys are going to be mapped to the value selected. So our dairy has been mapped to non-vegan, vegetables has been mapped to vegan, and fruit has been mapped to vegan. And…

Contents