Pandas how to Replace value based on condition

In this post we will learn, we will learn in Pandas how to replace value based on condition by using the numpy library function np.where() and Pandas dataframe loc[], mask[], where[] method. These Programs have used Pandas and NumPy library so please make sure these are installed on the system before running these programs.

1. Pandas Replace Value based on Condition


In this Python program, we are replacing values based on conditions by using the numpy library np. where() method To replace the value of columns Name of Pandas dataframe we have checked where Name ==’Jack’ and replace it by “Ton”

import pandas as pd
import numpy as np
    
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,98,100,100],
    'Subject': ['Math', 'Math', 'Math', 'chem']
}
 
dfobj = pd.DataFrame(data)

 
  
dfobj['Name'] = np.where((dfobj.Name == 'Jack'),'Ton',dfobj.Name)



print('Replaced  value in Dataframe:\n',dfobj )

Output

Replaced  value in Dataframe:
     Name  Marks Subject
0    Ton    100    Math
1   Rack     98    Math
2    Max    100    Math
3  David    100    chem

2. Replace value based on condition using df. loc[]


In this Python program, We will learn how to replace values In Pandas dataframe based on conditions with the help of the dataframe loc[] method. we have replaced values for column Name by checking condition dfobj.Name == “Jack” with “Ton”.

import pandas as pd
    
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,98,100,100],
    'Subject': ['Math', 'Math', 'Math', 'chem']
}
 
dfobj = pd.DataFrame(data)





dfobj.loc[(dfobj.Name== 'Jack'),'Name']='Ton'
print('Replaced  value in Dataframe:\n',dfobj )




Output

Replaced  value in Dataframe:
     Name  Marks Subject
0    Ton    100    Math
1   Rack     98    Math
2    Max    100    Math
3  David    100    chem

3. Pandas replace the value of entire row using df. loc[]


In this python program, We are replacing the values of Row2 all values with the value ‘Math’ by using dataframe.loc[] method.Let us understand with below example.

dfobj = pd.DataFrame(data,index=['Row','Row1','Row2','Row3'])


dfobj.loc['Row2'] = 'Math'

print('Replaced  value in Dataframe:\n',dfobj )

#based on condition

Output

Replaced  value in Dataframe:
        Name Marks Subject
Row    Jack   100    Math
Row1   Rack    98    Math
Row2   Math  Math    Math
Row3  David   100    chem

4. Pandas Replace the value of the entire column


In this Python program we are replacing value of entire “Marks” column with 90. It will replace all values of Math column with 90

dfobj = pd.DataFrame(data)

dfobj.loc[:, 'Marks'] = 90




print('Replaced  value in Dataframe:\n',dfobj )

Output

Replaced  value in Dataframe:
     Name  Marks Subject
0   Jack     30    Math
1   Rack     30    Math
2    Max     30    Math
3  David     30    chem

Replace the value of the column based on the condition


In this Python program, we have replaced Marks values based on the condition where Marks == 98 is replaced with 100.

dfobj.loc[dfobj["Marks"] == 98, "Marks"] = 100

print('Replaced  value in Dataframe:\n',dfobj )

Output

Replaced  value in Dataframe:
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100    Math
3  David    100    chem

5. Pandas Replace value based on condition using df.mask()


In this Python program, we have replaced the value of column Name by using condition Name == “Jack” with “Ton” by using the dataframe. mask() it replaces the value where the condition is True and keeps the original value where the condition is False.

import pandas as pd

data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,98,100,100],
    'Subject': ['Math', 'Math', 'Math', 'chem']
}
 
dfobj = pd.DataFrame(data)

dfobj['Name'].mask(dfobj['Name'] == 'Jack', 'Ton', inplace=True)
 
print('Replaced  value in Dataframe:\n',dfobj )

Output

Replaced  value in Dataframe     
   Name  Marks Subject
0    Ton    100    Math
1   Rack     98    Math
2    Max    100    Math
3  David    100    chem

6. Replace value based on condition using df.where


In this Python program, we are replacing the values of the dataframe Name column based on condition by using Dataframe.where() that replaces the value where the condition is False and keeps the original value where the condition is True

import pandas as pd

    
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,98,100,100],
    'Subject': ['Math', 'Math', 'Math', 'chem']
}
 
dfobj = pd.DataFrame(data)



dfobj.Name = dfobj.Name.where(dfobj.Name == 'Jack', other= 'Ton')



print('Replaced  value in Dataframe:\n',dfobj )

Output

Replaced  value in Dataframe:
    Name  Marks Subject
0  Jack    100    Math
1   Ton     98    Math
2   Ton    100    Math
3   Ton    100    chem

Summary

In this post, we have learned in Pandas how to Replace values based on conditions by using numpy and Pandas library functions.