Home > front end >  Replace values in dataframe using equal condition
Replace values in dataframe using equal condition

Time:09-16

df1 = pd.DataFrame({'Name1':['A','B','D','B','B','C','C','C','E','E','E'],
                    'Name2':['A','B','D','C','D','D','A','B','A','B','C'],'Marks2':[30,40,6,50, 88,'Nan',140,9,'Nan',65,70]})
df1

need to replace zero at all the values where name1 and name2 are equal. Like, if name1=name2 then need to replace marks2 with zero.

CodePudding user response:

Try this

df1['Marks2'].mask(df1['Name1'] == df1['Name2'] , 0 , inplace = True)

Hope it works for you.

CodePudding user response:

using loc :

df1.loc[df1.Name1 == df1.Name2 , 'Marks2'] = 0
  • Related