Replace values in dataframe using equal condition

Question:

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.

Asked By: Jack

||

Answers:

Try this

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

Hope it works for you.

Answered By: Echo

using loc :

df1.loc[df1.Name1 == df1.Name2 , 'Marks2'] = 0
Answered By: eshirvana
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.