Get startedGet started for free

Replace values using lists

1. Replace values using lists

Welcome back! In the previous lesson, we saw how we can use the .replace() function to replace scalar values more efficiently. In this one, we will see how we can use lists to replace single or multiple values.

2. Replace multiple values with one value

We already know how to replace a scalar value in a pandas DataFrame. However, there might be cases where we need to replace multiple values at the same time. Let's see how to do this. We will keep using the names dataset. Our objective is to change all ethnicities classified as WHITE NON HISPANIC or WHITE NON HISP to WNH. Using the .loc() function, we will locate babies of the ethnicities we are looking for, using the 'or' statement (which in Python is symbolized by the pipe). We will then assign the new value. As always, we also measure the CPU time needed for this operation.

3. Replace multiple values using .replace() I

Let's try to speed up this process using pandas .replace() function. As we saw in the previous lesson, the syntax of the .replace() function is very simple. We first type what we want to replace, and then what we want to replace it with. Instead of a scalar, we choose to replace a list of values with a single value. We want to perform the same task as in the previous slide, which is to replace all ethnicities classified as WHITE NON HISPANIC or WHITE NON HISP to WNH. Thus, in the .replace() function, we pass the values we want to replace, WHITE NON HISPANIC and WHITE NON HISP, as a list, and the value we want to replace them with as WNH. As we can see, there is a massive improvement in terms of efficiency by using the .replace() function.

4. Replace multiple values using .replace() II

So, can we use the .replace() function to replace multiple values with multiple values? Of course we can! As we have seen in the previous slides, there are some entries in our popular names dataset with ethnicity classified as BLACK NON HISP and WHITE NON HISP. We want to replace these entries with BLACK NON HISPANIC and WHITE NON HISPANIC respectively. One possible solution would be to use two different .replace() functions, but there is a more efficient way. Instead, we use one .replace() function which takes as input a list containing the entries we want to replace, BLACK NON HISP and WHITE NON HISP, and a list containing the values we want to replace them with, BLACK NON HISPANIC and WHITE NON HISPANIC. The function works on a one-to-one mapping, so the first element of the first list will be replaced by the first element of the second list, etc.

5. Let's do it

Now it's your turn to replace several values at once!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.