How to Replace Multiple Values using Pandas?

Pandas Replace Multiple Values

When one can analyse data using Python, does it give any flexibility to play around with the input data fed for the analysis? This is what this article set out to explore. We shall construct data & demonstrate replacing multiple values within it by leveraging the capabilities of the Pandas library.

Setting up the Pandas dataframe to replace multiple values

Let us start things first by importing the Pandas library to the active Python window by typing,

import pandas as pd

Now let us create a data frame using the indexing option. What indexing does is provide the provisions of including a header for each column of data during the time of their creation. In this example, we shall list the names of some snacks & also their corresponding price. In order to do that, the following code is to be constructed.

Data={'Snacks':('kamarkattu','then mittai','mysore pak','murukku'),'Price(INR)':[2,1,5,2]}

Bear in mind the use of flower brackets {} as the primary parentheses in the above code since only this would make Python understand that we are feeding datasets for analysing.

df=pd.DataFrame(Data)

It is to be noted that ‘D’ & ‘F’ ought to remain capitalised while typing DataFrame in the above code, else one might face the following error.

DataFrame Error
Data Frame Error

Once done, view the data fed using the print() command as shown below.

print (df)
Viewing The Dataset
Viewing The Dataset

Replacing Multiple Values in a Pandas Dataframe

Now let’s say one seems to dislike the snacks listed above & would like to fetch some alternatives in the same price range for replacing those. This can be done by using the vals_to_replace function whose syntax is given below.

vals_to_replace = {‘old value_1’:’new value_1’, ‘old value_2’:’new value_2’, ……..}

Once the code is typed, hit ENTER & view again the data fed through the print() command to see how the changes have been done.

Values Not Replaced
Values Not Replaced, but why?

The value replacement has been done by asking Python to substitute each of the previously listed values with the new ones which have been given in the code & the fact that the triple arrowheads have appeared in the next line also tells us there were no errors before the data has been viewed using the print() command.

But, what hasn’t been done is that the values substituted have never been mapped to the data frame! One can very well do this by using the following syntax,

df[‘column header’]=df[‘column header’].map(vals_to_replace)

where, column header – name of the column given during indexing

Applying the above syntax to the data set being used, it becomes evident that the column under which the entries are to be replaced is the ‘Snacks’ column. So, the same is to be updated to construct the code as shown below.

df['Snacks']=df['Snacks'].map(vals_to_replace)

Now, hit ENTER & view the replaced values by using the print() command as indicated in the below image.

Multiple Values Replaced
Multiple Values Replaced

Summary

Now that we have reached the end of this article, hope it has elaborated on how to replace multiple values using Pandas in Python. Here’s another article which details the usage of delimiters in read_csv() in Pandas. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are in looking to level up in Python. Cheers!