replace column values in pandas

How to Replace Values in Pandas DataFrame

Python Pandas is a powerful and popular library to analyze data in Python. It provides many useful functions to work with data. It uses DataFrame to store and organize data in Python. This dataframe is like table with rows & columns, with indexes and methods for further processing. Often you may need to replace values in Pandas Dataframe. In this article, we will learn how to replace values in Pandas DataFrame.


How to Replace Values in Pandas DataFrame

Here is the basic syntax to replace values in Pandas DataFrame. We will use replace() function for this purpose.

df['column name'] = df['column name'].replace(['old value'],'new value')

For example, let us say you have the following python dataframe.

import pandas as pd

colors = {'first_set':  ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
          'second_set': ['Yellow','Yellow','Yellow','White','White','Blue','Blue','Blue']
         }

df = pd.DataFrame(colors, columns= ['first_set','second_set'])

print (df)

Let us say you want to replace single value, that is, ‘Blue’ with ‘Green’ in ‘first_set column. Here is the python command for it. We specify the new value as a single string.

df['first_set'] = df['first_set'].replace(['Blue'],'Green')

If you want to replace multiple values, then here is the command for it. In this case, you mention the multiple values in the list for old values, as shown below.

df['column name'] = df['column name'].replace(['1st old value','2nd old value',...],'new value')

For example, if you want to ‘Blue’ and ‘Red’ colors with ‘Green’ color, then here is the command for it.

df['first_set'] = df['first_set'].replace(['Blue','Red'],'Green')

Let us say you want to replace multiple values, with multiple values such that 1st old value is replaced by 1st new value, 2nd old value is replaced by 2nd new value, and so on. Here is the command for it. In this case, we will mention the list of new values as second argument of replace() function.

df['column name'] = df['column name'].replace(['1st old value','2nd old  value',...],['1st new value','2nd new value',...])

For example, if you want to replace ‘Blue’ and ‘Red’ with ‘Green’ and ‘White’ respectively then here is the command for it.

df['first_set'] = df['first_set'].replace(['Blue','Red'],['Green','White'])

In this article, we have learnt several different ways to replace one or more values in Pandas Dataframe with one or more values.

Also read:

How to Add New Column to Existing DataFrame
How to Change Element Class Property Using JavaScript
How to Get Data Attribute in JavaScript
How to Shuffle Array in JavaScript
How to Remove Local Timezone from Date in JavaScript

Leave a Reply

Your email address will not be published.